Jindřich Širůček
Jindřich Širůček

Reputation: 344

How to send a post request with an array in payload by urlfetchapp

I need to simulate post request from a html Form to a page. I need to know, how to properly send an array in payload. Thanks in advance..

I have easy html code which will be on submit received by target page in this way:

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "Link[]": [
      "picturelink1.png", 
      "picturelink2.png", 
      "picturelink3.png"
    ], 
    "animationtype": "link", 
    "size": "400", 
    "speed": "0.4", 
    "usersize": "", 
    "userspeed": "0.1"
  }
}

I have written code in GAS, which should simulate the same request:

var payload =
      {       
        "Link[]":["jovianarchive_com/Content/Charts/621903744000000000_png","jovianarchive_com/Content/Charts/621903708000000000_png","jovianarchive_com/Content/Charts/621903816000000000_png"],  
        "animationtype":"link","size":"300","speed":"1","usersize":"","userspeed":""
      }

   var options =
   {
     "method" : "post",
     "payload" : payload
   };
  var result = (UrlFetchApp.fetch("http://httpbin.org/post", options).getContentText());

and this returns this request:

{
  "args": {},    
  "data": "",
  "files": {},
  "form": { "Link[]": "[Ljava.lang.Object;@536733f2",
  "animationtype": "link",
  "size": "300",
  "speed": "1",
  "usersize": "",
  "userspeed": ""
}

The problem is here:

"form": { "Link[]": "[Ljava.lang.Object;@536733f2",

there should be this instead:

  "form": {
    "Link[]": [
      "picturelink1.png", 
      "picturelink2.png", 
      "picturelink3.png"
    ], 

I have also tried to stringify it and send as JSON

"Content-type" : "application/json",

but it not produces desired output neither..

It receives payload as quoted string:

 "args": {}, "data": "", "files": {}, "form": { "{\"Link[]\":[\"jovianarchive_com/Content/Charts/621903744000000000_png\",\"jovianarchive_com/Content/Charts/621903708000000000_png\",\"jovianarchive_com/Content/Charts/621903816000000000_png\"],\"animationtype\":\"link\",\"size\":\"300\",\"speed\":\"1\",\"usersize\":\"\",\"userspeed\":\"\"}": "" }, .....

thx for any help..

Upvotes: 5

Views: 2909

Answers (3)

Noam Gal
Noam Gal

Reputation: 1292

Google's documentation suggests a very comfortable solution using JSON.stringify().

From their docs:

// Make a POST request with a JSON payload.
var data = {
  'name': 'Bob Smith',
  'age': 35,
  'pets': ['fido', 'fluffy']
};
var options = {
  'method' : 'post',
  'contentType': 'application/json',
  // Convert the JavaScript object to a JSON string.
  'payload' : JSON.stringify(data)
};
UrlFetchApp.fetch('https://httpbin.org/post', options);

P.S. If you'd like to parse the response data as an object, you can use JSON.parse() (you should probably check the response code as well):

  var response = UrlFetchApp.fetch('https://httpbin.org/post', options);
  var responseData = JSON.parse(response.getContentText()); // an object

Upvotes: 0

Eugene Fedorov
Eugene Fedorov

Reputation: 1

Also you can use a construction like this if you need send array with (key => value):

payload = {
    'fields[PRICE]': 2700
    ,'fields[NAME]': 'Name'
};

// to change it
payload['fields[PRICE]'] = 2845;

Upvotes: 0

Jindřich Širůček
Jindřich Širůček

Reputation: 344

I have found it!!! haleluya.. the payload has to be prepared in this way! I have found this solution during I was making a workaround PHP script.

  var payload =
      {       
        "Link[0]":"http://..",
        "Link[1]":"http://..",        
        "animationtype":"link","size":"300","speed":"1","usersize":"","userspeed":""
      }...

Not very intuitive, but it works great!

Received payload doesnt look like the same like from a HTML form, but in my case it worked!

{ "args": {}, "data": "", "files": {}, "form": 
{ 
"Link[0]": "http://jovianarchive.com/Content/Charts/621903744000000000_.png", 
"Link[1]": "http://jovianarchive.com/Content/Charts/621903708000000000_.png",
"animationtype": "link", "size": "300", "speed": "1", "usersize":` "","userspeed": "" }, .....

Upvotes: 2

Related Questions