Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

302 Redirect Blues

74 views
Skip to first unread message

Shaun H

unread,
Feb 24, 2025, 5:25:53 AMFeb 24
to Google Apps Script Community
I keep getting a "Moved Temporarily" (302 redirect) response when sending a basic curl command to a very simple doPost.

The project test is very simple - this is the entire Code.gs code:
function doPost(e) {
  return ContentService.createTextOutput(JSON.stringify({ message: "Received" }))
    .setMimeType(ContentService.MimeType.JSON);
}

I have tried creating both a personal and a workgroup Google account, both getting the same results.

I have tried it from a different network (home/personal).

The executions logs are not showing details - just that they have been "completed" without failing - but from the curl command side, a 302 is deadly.

I have set project settings with:
Execute as: Me
Who has access: Anyone
GCP Project: default

Here's the curl command I have used (with my id removed):
curl -X POST "https://script.google.com/macros/s/[removedforsecurity]/exec?action=processData" -H "Content-Type: application/json" -d "{\"upcCode\":\"089036422802\"}"

I have also tried:
curl -X POST "https://script.google.com/macros/s/ [removedforsecurity]  /exec" -H "Content-Type: application/json" -d "{\"upcCode\":\"089036422802\"}"

curl response is:
<!-- GSE Default Error -->
<H1>Moved Temporarily</H1>

The Execution logs are no help either; they report as completed without failing, but they also do not allow me to click on them either.  Even when I add a line: Logger.log("received") as the first line in the doPost.

Any thoughts as to how this can be resolved?

Thanks!

DimuDesigns

unread,
Feb 24, 2025, 5:44:27 AMFeb 24
to Google Apps Script Community
HTTP POST requests always redirect (302) for GAS Web Apps. 

The client making the request has to be able to follow the redirect to retrieve your JSON response payload - if it can't you're pretty much out of luck.

If you need to handle POST requests without a redirect then you have to leverage something other than GAS.

One option is to use a Google Cloud Run Function instead.

Google Pony

unread,
Feb 25, 2025, 1:58:49 PMFeb 25
to Google Apps Script Community
Solution to Google Apps Script 302 Redirect Issue

Q) Why It Happens?
A) Google Apps Script Web Apps always return a 302 redirect for POST requests. Some clients (like curl) do not follow redirects by default.

Recommended Solutions:

1. Use -L in Curl to Follow Redirects
curl -X POST -L "https://script.google.com/macros/s/[removed]/exec" \

     -H "Content-Type: application/json" \
     -d "{\"upcCode\":\"089036422802\"}"

2. Use fetch() in JavaScript (Auto-Follows Redirects)
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ upcCode: "089036422802" })
}).then(res => res.json())
  .then(data => console.log(data));

3. Switch to GET (If Possible)
 Modify Code.gs:
const doGet = (e) => ContentService.createTextOutput(JSON.stringify({ upcCode: e.parameter.upcCode })).setMimeType(ContentService.MimeType.JSON);

Use GET request:
curl -X GET "https://script.google.com/macros/s/[removed]/exec?upcCode=089036422802"

4. Use Google Cloud Run (For Full Control)
 If you need strict control over POST responses, migrate to Google Cloud Run.
Best Option: Use -L in curl or fetch() in JavaScript.
 For API control, consider Google Cloud Run.


Sincerely yours,
Sandeep Kumar Vollala
Developer - India
LinkedIn Logo WhatsApp Logo
Reply all
Reply to author
Forward
0 new messages