Coding a shortcut Webview/Problem with the API
-
Hi,
I’m currently coding a simple-to-use-Webview for my colleagues, it will just contain some buttons to deploy images on hosts/groups. And at this point I have a problem. I want to call the deploy action but I get only this response:
{ "error": "Invalid tasking type passed" }
Followed the documentation on here (https://docs.fogproject.org/en/latest/Knowledge-Base/integrations/api/?h=api#put) so I set the taskType as “1”. I’m testing it in Postman. Other get examples are working, so the token are right.
Does anyone know what’s the problem? Thank you in advance.
-
@sega What’s the api endpoint you’re reaching to?
Right now we just see you having some request, but what is the request you’re making exactly?
-
The endpoint I’m trying to reach is: http://192.168.0.0/fog/host/128/task
And as I read in the documentation I have to add taskType: 1 as the body. So basically I just need to know which ID the task “Deploy” has. -
@sega I would also recommend following this information:
https://news.fogproject.org/simplified-api-documentation/While it’s a bit older, it seems the other link you’re using is incomplete at this point.
-
@Tom-Elliott I saw this site also and used the example at the end of the page:
curl -H 'fog-api-token: abcde' -H 'fog-user-token: fghij' -H 'Content-Type: application/json' -X POST -d '{"taskTypeID":13,"deploySnapins":6}' http://fogserver/fog/host/1/task
I used this one, removed the "deploySnapins and changed the taskTypeID into 1, which should stand for the task of deployment. Still the same error. There isn’t any documentation about the various taskTypeIDs, I think thats my main problem.
-
@sega I believe you need more in your json post
I have a function in the fogApi powershell module to deploy an image.
The code is in the syntax for powershell not curl, but it should give you an idea.
https://github.com/darksidemilk/FogApi/blob/master/FogApi/Public/Send-FogImage.ps1i.e. a simple deploy image now task of a host’s assigned image would have json like this
{ "taskTypeID": "1", "shutdown":"0", "other2":"0", "other4":"1", "isActive":"1" }
IIRC other2 is debug task and other4 is wake on lan
-
@sega said in Coding a shortcut Webview/Problem with the API:
There isn’t any documentation about the various taskTypeIDs, I think thats my main problem.
It’s on the todo list, it’s a long todo list though.
In the interim:
You can get the tasktype ids from the urls in the gui
i.e. you go to a hosts task page
http://fogserver/fog/management/index.php?node=host&sub=edit&id=1#host-tasksAnd each link to a task type on that page ends with
&type=#
where that number is that tasks id.To then figure out the required fields for that task you have to look into the various classes. For tasks its a bit trickier to find that it is for objects. I sadly don’t remember how I figured out where those fields were, I think @Tom-Elliott helped me a few years back and I forgot to write down the origin of it all.
-
Ok, the call in Postman worked now, I just had to change a setting.
Now I want to import that call in html/js. But I just get an error as response:GET http://192.168.xxx.xxx/fog/task/active net::ERR_ABORTED 403 (Forbidden)
I’m pretty sure I have to change something in the fetch function? But I’m not sure. The HTML file is right now running on a local PHP server.
fetch("http://192.168.xxx.xxx/fog/task/active", { method: "GET", mode: "no-cors", headers: { "fog-api-token": "token", "fog-user-token": "token" } })
-
@sega said in Coding a shortcut Webview/Problem with the API:
Ok, the call in Postman worked now, I just had to change a setting.
Now I want to import that call in html/js. But I just get an error as response:GET http://192.168.xxx.xxx/fog/task/active net::ERR_ABORTED 403 (Forbidden)
I’m pretty sure I have to change something in the fetch function? But I’m not sure. The HTML file is right now running on a local PHP server.
fetch("http://192.168.xxx.xxx/fog/task/active", { method: "GET", mode: "no-cors", headers: { "fog-api-token": "token", "fog-user-token": "token" } })
Does this fetch function work for other things in your custom page?
I’m not a php expert, but I found this post that suggests using a curl extension or using file_get_contents with json_encode and json_decode
https://stackoverflow.com/questions/9802788/call-a-rest-api-in-php
@Tom-Elliott might know more about using php with the api.
-
This should work (worked at the start), but that’s a good point. I will test it with another API just to get sure.
The HTML file just runs on a PHP server but has no PHP in it, just HTML and JavaScript. But won’t be a big problem, to rewrite it in PHP, if that’s necessary. -
I changed the code to PHP and now it works, so I think it was something security related from the browser.