Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Rest API Response in browser is always in XML instead of JSON

apaluri
Giga Contributor

Am writing a simple scripted rest api to place order using cart API.The response in servicenow   rest api explorer is in JSON,

but when i run in browser, it always shows up in xml, i have seen many others had this question and is unanswered.Amy pointers will help

Thanks,

Anirudh

3 REPLIES 3

jarodm
Mega Guru

The first thing I would look at is the request headers. Set it something similar to "Accept=application/json"



If one is not set, it may default to XML.


apaluri
Giga Contributor

Hello Jarod,



I did try setting these in my script but didnt help



request.setRequestHeader("Accept","application/json");


  request.setRequestHeader('Content-Type','application/json');


apaluri
Giga Contributor

I figured this out from docs.servicenow.com,



used var writer = response.getStreamWriter() and built the object and converted in the end using global.JSON.stringify(responseObj)



Thanks,


Anirudh