4
votes

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..

2
Clarification: are you trying to send "application/x-www-form-urlencoded" data ? - Karan
Im trying to send an array with 3 strings in it.. viz: var payload = { "Link[]":["jovianarchive.com/Content/Charts/…, "animationtype":"link","size":"300","speed":"1","usersize":"","userspeed":"" } - Jindřich Širůček
wouldn't JSON work for you ? that way you could easily use {"Link":["file1.png", "file2.png", "file3.png"] } You only need to set the "Content-type" to "application/json" and you can use JSON.stringify on the object. - Karan
@Karan that is what I have tried also.. but I got here: - Jindřich Širůček
@Karan that is what I have tried also.. but 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\":\"\"}": "" }, ..... but it should be like this: "form": { "Link[]": [ "picturelink1.png", "picturelink2.png" ] } - Jindřich Širůček

2 Answers

1
votes

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": "" }, .....
0
votes

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;