doPost(e) does not return parameters but doGet(e)

2020-03-07 08:06发布

问题:

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?

回答1:

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

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



回答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'});


回答3:

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.