Skip to main content
Solved

API response is JSON instead XML

  • April 1, 2018
  • 5 replies
  • 53 views

anverhousseini
Forum|alt.badge.img+11

Hi

I wrote a script which makes a GET request. Like this:

curl -H "content-type: application/xml" -s -o "/private/tmp/computers.xml" -fku "username":"password" "https://jamf.example.com/JSSResource/computers" -X GET

If the inventory contains 0 computers I get a JSON response. If there are more than 0 computers the output is XML, which is correct. This happens only on Jamf Pro 10.3.

Is this a product issue or is my curl command wrong? I even tried to force a XML response by defining the header.

Best answer by chriscollins

You are using the wrong header to specify you want XML. You use “Content-type:” when telling the server what to expect from you when you are sending to the server. You should use “Accept:” to tell the server what you expect to receive back when asking for information.

5 replies

Forum|alt.badge.img+16
  • Honored Contributor
  • 404 replies
  • Answer
  • April 1, 2018

You are using the wrong header to specify you want XML. You use “Content-type:” when telling the server what to expect from you when you are sending to the server. You should use “Accept:” to tell the server what you expect to receive back when asking for information.


anverhousseini
Forum|alt.badge.img+11
  • Author
  • Valued Contributor
  • 98 replies
  • April 2, 2018

@chriscollins This was the solution, thank you! But shouldn't be XML by default?


talkingmoose
Forum|alt.badge.img+36
  • Community Manager
  • 1913 replies
  • April 2, 2018

My understanding is that XML use to be the default, but a change in Java a couple years ago caused the default to change to JSON. Regardless, good form is to specify the content type in your curl command that you'll accept.


Forum|alt.badge.img+12
  • Contributor
  • 37 replies
  • April 2, 2018

I remember a few versions ago (I think 9.96 or 9.98) the default for many things changed from xml to json and I had to update my scripts to explicitly request xml. And yet now, on 9.101, I just ran some tests and can't find anything that defaults to json. Hmm. Maybe it was just a bug.

In any case, it seems prudent to explicitly request the data type as a matter of habit.


mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • 7886 replies
  • April 2, 2018
In any case, it seems prudent to explicitly request the data type as a matter of habit.

This is exactly what I do now with all API scripts, even if I'm almost 100% certain the default response will be XML. I explicitly specify it anyway, just to be doubly sure.