3

Nowhere on the internet is this specific problem or a fix for it mentioned, so here goes:

My app contains the following doGet() and doPost() functions:

function doGet (e){return ContentService.createTextOutput("User says: "+JSON.stringify(e))}
function doPost(e){return ContentService.createTextOutput("User says: "+JSON.stringify(e))}

GET http://*published URL*/+params returns:

User says:     
{
  "queryString":"testparamA=abc&testparamB=bcd&testparamC=cde",
  "parameter":
    {
      "testparamA":"abc",
      "testparamB":"bcd",
      "testparamC":"cde"
    },
  "contextPath":"",
  "parameters":
    {
      "testparamA":["abc"],
      "testparamB":["bcd"],
      "testparamC":["cde"]
    },
  "contentLength":-1
}

Whereas, POST http://*published URL*/+params returns:

User says:
{
  "queryString":null,
  "parameter":{},
  "contextPath":"",
  "parameters":{},
  "contentLength":0
}

My goal is to access the POST parameters. But something seems to be blocking the script from fetching them when transmitted using the POST method. GET seems to work just fine.

What am I missing and what is the solution?

Let Me Tink About It
  • 15,156
  • 21
  • 98
  • 207
  • 2
    Can you share the code that POSTs to this webapp? Its possible that is not doing the right thing. POST can be tough to visualize as its not just appending a query string to the URL – Arun Nagarajan Nov 27 '12 at 19:32
  • 2
    For example, `curl -L --data "test=testval&test2=test2val" https://script.google.com/macros/s/AKfycbxFF7bnG9AwQFmjUCB88b2Pzoz7Em_HBRsXpuxi2wdHNzmZwDNK/exec` will respond with the right values. – Kalyan Reddy Nov 27 '12 at 19:40
  • @KalyanReddy That URL throws a 404 NOT FOUND error when I try. Could you please make that URL accessible to "anyone?" – Let Me Tink About It Nov 28 '12 at 02:09

3 Answers3

8

POST and GET works perfectly fine for me using the exact same code you posted. My guess is that you are not testing the POST correctly.

Here is my published URL. The code behind is a copy/paste of your sample -

https://script.google.com/macros/s/AKfycbzWZv9WUp7rUtOxZhhwuXPpuNXuvPGpiHyrcYrPeNLiusOWzazo/exec

Test it from http://hurl.it (simple REST tester in a browser) and works without any problem.

Request (make sure to check follow redirects) -

request screenshot

Response (response after the redirect for one time ContentService URL for both GET and POST)

response screenshot

Arun Nagarajan
  • 5,547
  • 1
  • 22
  • 19
  • Yes, you proved the code works and the problem exists with my POST operation. Good job and thank you. Also, I appreciate learning about this new hurl resource. I'm sure it will come in handy in the future. (I had been using the FF add-on, Poster.) Cheers! =] – Let Me Tink About It Nov 28 '12 at 05:45
2

I think this is somewhat expected. When one does an HTTP GET the parameters are passed on the url. When it's a POST they go on the payload, not the url.

Try calling post like this (Apps Script code):

UrlFetchApp.fetch(scriptUrl, {method:'post', payload:'param1=value1&param2=value2'});
Henrique G. Abreu
  • 17,406
  • 3
  • 56
  • 65
  • Yes, I am fairly certain I did the POST correctly. I did put the parameters in the payload for POST just as you described. Please test it yourself. I think you might see what I mean. If you get your test to work, please let me know. Otherwise you might see for yourself that the POST isn't working as expected. – Let Me Tink About It Nov 28 '12 at 03:57
  • Also, in your code, don't quotes go around the method and payload keys? And the payload value in object form? As follows: UrlFetchApp.fetch(scriptUrl, {"method":'post', "payload":{"param1":"value1","param2":"value2"}}); – Let Me Tink About It Nov 28 '12 at 04:01
  • Quotes are not required if the property name is a valid javascript identifier. In other words, you only need quotes if the property name has spaces, or starts with a number, etc. And I just tested it and my code worked just fine. My `doPost` got the parameters correctly. – Henrique G. Abreu Nov 28 '12 at 10:21
0

I am having the same issue however. doGet(e) returns a parameters collection, but doPost(e) does not. Looking at the value e in the script log:

DoGet logs this:

{queryString=param1=value1&param2=value2&param3=value3, 
parameter={param1=value1, param2=value2, param3=value3}, 
contextPath=, 
parameters={param1=[value1], param2=[value2], param3=[value3]},contentLength=-1}

DoPost logs this:

{queryString=lib=Ma1kfpb2uwfs976NQh3S0GV_Vnss8VuKo&appId=u33198874110&formId=xxxxxxxxxxxx&token=AJuLMu2XVXMgpvS-7l6mWLVDmxjYMA6ZEQ:1393694820550&gm1xs=&service=AKfycbzfP8gYQknL9dNG6SVf0LmPYy3xiEAtyFQ8AvJDwfs, 
parameter={gm1xs=, lib=Ma1kfpb2uwfs976NQh3S0GV_Vnss8VuKo, appId=u33198874110, param1=value1, formId=u33198874111, token=AJuLMu2XVXMgpvS-7l6mWLVDmxjYMA6ZEQ:1393694820550, param2=value2, param3=value3, service=AKfycbzfP8gYQknL9dNG6SVf0LmPYy3xiEAtyFQ8AvJDwfs},
contextPath=, contentLength=341}

So in the doGet it is trivial to loop through e.parameters, but in doPost you must loop through e.parameter instead, and deal with the other parameters from the form that you don't care about.

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Gentry
  • 1
  • 1