Posted on 08-29-2013 10:44 PM
I've been doing some work with the API and I'm trying to update the location information of a computer. I can successfully make the appropriate request using XML, but I'm unable to do the same with JSON.
Setting Content-Type to "application/json" results in getting a response with the following message:
"Unsupported Media Type,
The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method"
Is the API unable to handle JSON puts/posts or is there something I'm doing wrong?
For reference I'm making the request in objective-c using the ASIHTTPRequest class. here is the JSON I'm sending:
{
"computer":{
"general": {"id":265},
"location":{"room":"IT Room"}
}
}
Solved! Go to Solution.
Posted on 10-14-2013 05:17 PM
Incase anyone else is looking for an answer, it got onto support and they told me that the API does not support JSON for anything but GET requests. If you want to do put, post, etc you'll have to use XML.
It would have been nice if this was actually documented somewhere. Additionally, the only only reason I was able to construct the xml required for the put/post request is because I can partially remember how to do it from the pre 9.# documentation.
Posted on 08-30-2013 12:44 PM
Hi Nicholas,
Yes, the API does in fact support JSON. Depending on your Java libraries on your JSS, it may not want to default to JSON over XML. Like for example, I have seen RHEL servers with Java JRE/JDK 7 by default return JSON every time, but if you downgrade to Java JRE/JDK 6 it defaults back to XML.
The best way to address this, is probably to force the Application type in your curl command. You can use the -H switch in curl to change the return headers to XML or JSON. So for example:
curl -H "Accept: application/json"
This should always return JSON in your GET/PUT/POST from the REST API. Please post back if that does not work, or contact your account manager.
Thanks,
Tom
Posted on 08-30-2013 02:51 PM
Hi Tom, thanks for your reply.
I've already been doing exactly what you've suggested and have been receiving json perfectly fine. My problem is only with sending json back to the server. Is there an alternative header to "Content-Type" that I should be setting to let the server know that I'm sending json rather than xml, and is it something other than "application/json"?
Posted on 09-02-2013 05:25 AM
Nicholas,
Use AFNetworking vs ASIHTTPRequest; it's better for a lot of reasons. Plus, Ben killed it (ASIHTTPRequest) some time ago.
We use AFN classes all the time with JSS for JSON data transfers....works great with Java 6/7 and JSS 8/9.
Thanks,
j
Posted on 09-05-2013 03:55 PM
Thanks, I'm looking into AFNetworking.
In an effort to try and eliminate any variables, I'm trying to make the request first through Postman (a chrome app for testing http web service) and I'm still getting 415 unsupported media type errors. Below is a screenshot of all the settings I'm currently using, can anyone spot what I'm missing? Cheers
Posted on 10-14-2013 05:17 PM
Incase anyone else is looking for an answer, it got onto support and they told me that the API does not support JSON for anything but GET requests. If you want to do put, post, etc you'll have to use XML.
It would have been nice if this was actually documented somewhere. Additionally, the only only reason I was able to construct the xml required for the put/post request is because I can partially remember how to do it from the pre 9.# documentation.
Posted on 06-30-2015 10:24 AM
Booooooooooooooooooooooooooo. I use PHP/JSON because the libraries exist and I'm familiar.
Posted on 11-03-2017 07:46 AM
I agree; it's only because most every other web-app and management solution accepts JSON, which is the modern ReST industry 'best-practice'. Other than that.... no reason. BTW, When is the universal API coming? When will it be complete? These are the questions that even management is asking (hence, the increasing popularity of open-source solutions). When folks like Leslie H present at JNUC (https://github.com/BIG-RAT/JNUC2017.git), it's exciting, but it'd be nice to be able actually use it. Heck, even BigFix provides this (https://www.jokecamp.com/blog/invoke-restmethod-powershell-examples/ ). Sigh....