Reputation: 16132
I am running a scraping project from a headless browser using node.js and Puppeteer. I want to post the data to a Google Apps Script for further processing. I expect to see the data in my GAS project with populated parameters. But instead, I get the following result with only empty parameters.
https://script.googleusercontent.com/macros/echo?user_content_key=[key]{"parameter":{},"contextPath":"","contentLength":-1,"queryString":"","parameters":{}}
Here is the GAS code that generates that response.
Code.gsfunction doGet(e){
return handleResponse(e);
}
function doPost(e){
return handleResponse(e);
}
function handleResponse(e) {
var json = JSON.stringify(e)
var textOutput = ContentService.createTextOutput(json);
return textOutput
}
Here is the code I am using to send the request.
scraper.jsconst request = require('request');
request({
method: POST,
preambleCRLF: true,
postambleCRLF: true,
uri: postUrl,
multipart: {
chunked: false,
data,
},
},
I have verified using [RequestBin][1] that I am sending a valid POST request.
What am I doing wrong?
Upvotes: 0
Views: 5279
Reputation: 9581
Please review how you're publishing the web app.
The URL for your POST & GET should be something like https://script.google.com/macros/s/IDENTIFIER/exec
, not https://script.googleusercontent.com/macros/echo?user_content_key=[key]
. The latter URL looks like a redirect from publishing a web app that is not accessible to "Anyone, even anonymous".
If that's set correctly, this request
curl --request POST \
--url 'https://script.google.com/macros/s/IDENTIFIER/exec?param1=1¶m2=2' \
--header 'content-type: application/json' \
--data '{"json": true}'
returns
{
"parameter": {
"param1": "1",
"param2": "2"
},
"contextPath": "",
"contentLength": 14,
"queryString": "param1=1¶m2=2",
"parameters": {
"param1": [
"1"
],
"param2": [
"2"
]
},
"postData": {
"type": "application/json",
"length": 14,
"contents": "{\"json\": true}",
"name": "postData"
}
}
Upvotes: 1