Huachao / vscode-restclient

REST Client Extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=humao.rest-client
MIT License
5.25k stars 436 forks source link

Post call not working for google apps scripts doPost #468

Open lastlink opened 4 years ago

lastlink commented 4 years ago

Steps to Reproduce:

Create a new project in google apps script Add this code to a script, publish with anonymous permission.

function doGet(e) {
 var request = JSON.parse(JSON.stringify(e));
  return buildErrorResponse(request,200);
}

function doPost(e) {
  var request = JSON.parse(JSON.stringify(e));
  return buildErrorResponse(request,200);
}

function buildErrorResponse(message, code) {

  var output = JSON.stringify({
    status: 'error',
    code: code,
    message: message
  });

  return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.JSON);
}

A curl works

curl -d "" -L https://script.google.com/macros/s/AKfycbyW_I75HOxOrLhsm4x4ZJTF71VRP2-1Ey3gsijORAOywKGLG18/exec

remove the "" and it gives similar incorrect response

curl -d -L https://script.google.com/macros/s/AKfycbyW_I75HOxOrLhsm4x4ZJTF71VRP2-1Ey3gsijORAOywKGLG18/exec

<HTML>
<HEAD>
<TITLE>Moved Temporarily</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Moved Temporarily</H1>
The document has moved <A HREF="https://script.googleusercontent.com/macros/echo?user_content_key=5JVyAwDxUguj-RbYIr9ep9fJ_jX4rComNtUPz1nAl0vJvbOCWLJcgJYGGECPa10BnsxKxLRmHvKS9VizC6IJ51bwRlj_zf1hm5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnPQ-NNAGf-reMhKa49GYIy13feRkyntKDJT1G02xdYRamd8znjb4TYRDR4Evaeworpcdv3aNHr6K&amp;lib=M4gjLbtG2y_MJRH4TTygoOoj2lYWGeOP2">here</A>.
</BODY>
</HTML>

expected response

{"status":"error","code":200,"message":{"parameter":{},"contextPath":"","contentLength":0,"queryString":"","parameters":{}}}

solution:

I suspect something changed in the vscode version or the body is not attaching properly.

lastlink commented 4 years ago

After doing a copy curl from rest client I discovered that the --url doesn't work on windows while -L does work.

curl --request POST --url https://script.google.com/macros/s/xxx/exec --header 'content-type: application/json' --data '{}'

vs

curl --request POST -L https://script.google.com/macros/s/xxx/exec --header 'content-type: application/json' --data '{}'

Huachao commented 4 years ago

@lastlink could you please provide the actual request that not working for you?

lastlink commented 4 years ago
@macroId=AKfycbzTiRpq4ZqCSiR5JXbBQPtMUmwuRQbX-j4srBTnDIMDjQJywcs
###
POST https://script.google.com/macros/s/{{macroId}}/exec HTTP/1.1
Content-Type: application/json

{
    "machineId": "asdf1234"
}
Huachao commented 4 years ago

curl -d "" -L https://script.google.com/macros/s/AKfycbyW_I75HOxOrLhsm4x4ZJTF71VRP2-1Ey3gsijORAOywKGLG18/exec

Did you mean that when the request body is empty string in curl request that works for you? While in the above plain HTTP request, the request body is a JSON with machineId. Can you curl still succeed with the same request body?