RESTful from Google Apps Script

I invoke a RESTful webserice via a method GET url with paramters from google apps script successfully using the following:

UrlFetchApp.fetch(url);

But when I switch the method to use POST, the result returned tells that no parameter was sent:

UrlFetchApp.fetch(url, options);

I also tried to use Google Chrome's (Advanced) Rest Client, and the payload parameters are not sent there as well.

What am I missing?

Below is the Google Chrome's (Advanced) Rest Client interface.

enter image description here

Upvotes: 0

Views: 4095

Answers (1)

Cameron Roberts
Cameron Roberts

Reputation: 7367

You mention in your comments that you are converting the payload you send to JSON.

First, are you certain the remote service is expecting a JSON payload, rather than form encoded data?

If the service expects form encoded data, don't JSON.stringify your payload:

var data = {"x1": "1", "x2": "2"}; 
var options = {method : "post", payload : data};

If the service does expect a JSON payload, you need to set the content type:

var data = {"x1": "1", "x2": "2"}; 
data = JSON.stringify(data);
var options = {method : "post", payload : data, contentType:"application/json"};

Otherwise the default content type of 'application/x-www-form-urlencoded' is used.

See the description on "contentType" under "Advanced Parameters": https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app

Upvotes: 2

Related Questions