3

I'm having trouble getting Google App Script's UrlFetchApp class to correctly format a JSON payload in a HTTP request.

Here's how I'm formatting the payload:

var data = {
  'RequestData': {
    'ItemNumber': '1',
    'StockItemId': '2',
    'ImageUrl': 'www.example.com'
  }
};

var payload = JSON.stringify(data);

Surely enough, Logger.log(payload) returns:

{"RequestData":{"ItemNumber":"1","StockItemId":"2","ImageUrl":"www.example.com"}}

However, when I integrate this with the actual request:

var url = 'https://example.com/api/method';

var options = {
  'method': 'post',
  'contentType':'application/json',
  'payload': payload,
  'muteHttpExceptions': true
};

and examine Logger.log(UrlFetchApp.getRequest(url, options).toSource()), I get this:

({method:"post",
payload:"{\"RequestData\":{\"ItemNumber\":\"1\",\"StockItemId\":\"2\",\"ImageUrl\":\"www.example.com\"}}",
followRedirects:true,
validateHttpsCertificates:true,
useIntranet:false, contentType:"application/json",
url:"https://example.com/api/method"})

i.e. all the quotes are escaped, as if JSON.stringify is being called twice on the payload.

I thought GAS might be calling an equivalent of JSON.stringify on the payload behind the scenes because contentType is specified as "application/json" but if I pass the data raw without stringifying it, I get:

({method:"post",
payload:"RequestData=%7BItemNumber%3D1,+StockItemId%3D2,+ImageUrl%3Dwww.example.com%7D",
followRedirects:true,
validateHttpsCertificates:true,
useIntranet:false, contentType:"application/json",
url:"https://example.com/api/method"})

which is weird and (I'm pretty sure) an invalid JSON string. Either way, the request fails (400 Bad Request) and I'm pretty sure it's because I can't get the JSON across properly.

All the docs and examples I can find seem to suggest that JSON.stringify(payload) is the way to go but I'm clearly having issues...

Have also had the thought that this might have something to do with the implementation of the .toSource() method and that my problem might lie elsewhere but I have no way of knowing / otherwise being able to check the request body I'm sending.

Any insight would be much appreciated!

Julian
  • 31
  • 1
  • 4
  • You may refer in this SO thread: http://stackoverflow.com/questions/15675394/bad-request-with-urlfetchapp. It stated that if you can capture the HTTP Request hitting your server, there may be a clue to what is malformed. This could also be the result of a firewall. It might also because of [Content Security Policy header](http://stackoverflow.com/questions/36742212/google-apps-script-sheet-returns-bad-request-400). If it is set by the server and your origin doesn't satisfy its rules, then the browser will show error 400. – abielita Apr 12 '17 at 15:08
  • Might be an artifact as Logger will call the toString() on the object. JSON.strigify() is the proper way to generate application/json payload. Id dig further into the API of the service you are using. – Spencer Easton Apr 12 '17 at 19:35
  • Thanks for the tips guys. Turns out there was nothing wrong with my code... after a 2 hour long conversation with the developers of the web service, turns out the 'RequestData' parameter was named incorrectly in the docs for the API. Changing it to 'request' made everything work... frustrating waste of time but at least problem solved! – Julian Apr 12 '17 at 23:53

0 Answers0