AnsweredAssumed Answered

How to use esri.request to post JSON?

Question asked by Stryker99 on Jun 7, 2017
Latest reply on Jun 27, 2017 by lkoumis

I have been using the esri.request to make get calls for a while and it's worked great:

request = Request({url: url, handleAs: 'json'});

Now I have a requirement to be able to post JSON data and get a similar response. From the API reference I thought it would be as simple as:

request = Request({url: url, handleAs: 'json', callbackParamName: 'callback', content: params}, {usePost: 'true'});

But I was getting a 415 error from the REST endpoint I was hitting.

I looked at the developer tools, Network section, and I saw that under Params, the payload was of type Form data. I think it needs to be JSON format. I looked under Headers and Content-Type is "application/x-www-form-urlencoded".

So I found the setRequestPreCallback function and started using that to set the headers["Content-Type"] = "application/json". That seemed to work because I can see Content-Type is now "application/json". However, my 415 error changed to a 400 error. If I look under Params it is not type JSON like I expect. It just says Request payload and the data looks like a string with = and & instead of a JSON format.


Is there something else you have to do to make the data post as JSON?