Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A POST to a Google Apps Script web app returns 405 or a CORS error

ErrorSorry, the file you have requested does not exist.

Short answer

Apps Script web apps run doPost for POST and doGet for GET, and the Apps Script guides describe no other entry points. Check for the reserved parameter names c and sid first, which can give a 405. From a browser, send the body as text/plain so no preflight is needed, and make your client follow redirects.

  • Applies to Google Apps Script web app deployment, doPost with ContentService, current Chrome and Firefox
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Send the POST from the command line, following redirects, with a text/plain body. If this returns your data, the script is fine and the cause is the browser side (preflight or redirect handling). If it returns 405 too, look at your parameter names. Use the /exec URL of the deployment.

    curl -L "$WEB_APP_URL" \
      -H "Content-Type: text/plain" \
      -d '{"name":"test"}'
  2. 2Match what you see, then jump to its fix

    1. 1HTTP 405 with the message Sorry, the file you have requested does not exist.The request uses the reserved parameter names c or sidFix
    2. 2The Network tab shows an OPTIONS request that fails, and the request carries Content-Type: application/json or a header such as AuthorizationThe browser sends a preflight OPTIONS request firstFix
    3. 3The response is a redirect, or a page that is not your JSON, and the same call with curl -L returns the dataThe client does not follow the redirect to script.googleusercontent.comFix

What does the error look like?

  • Sorry, the file you have requested does not exist.
  • Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
  • The failing row in the browser's Network tab is an OPTIONS request, and doPost never runs

Cause 1 of 3

The request uses the reserved parameter names c or sid

1Check

The Apps Script web app guide reserves the parameters c and sid, and says using them can produce an HTTP 405. Look for query or form fields with those names in the client.

grep -rnE "[?&](c|sid)=|['\"](c|sid)['\"][[:space:]]*:" src/

2Fix

Rename the fields, for example category and sessionId, in the client and in the script that reads them.

// client
const url = `${WEB_APP_URL}?category=books&sessionId=${id}`;

// Apps Script
function doPost(e) {
  const category = e.parameter.category;
  return ContentService.createTextOutput(category);
}

3Verify

The same request, with the renamed parameters, no longer returns 405.

curl -L "$WEB_APP_URL?category=books"

Confirmed by developers.google.com

Did this fix it?

Cause 2 of 3

The browser sends a preflight OPTIONS request first

1Check

A cross-origin POST needs no preflight only if it carries just safelisted headers, and its Content-Type is application/x-www-form-urlencoded, multipart/form-data or text/plain. JSON content types and custom headers trigger an OPTIONS request first, and the web app guide documents only doGet and doPost as handlers. Look at the headers your client sets.

grep -rn "Content-Type\|Authorization\|headers:" src/

2Fix

Send the JSON as a plain-text body, keep the headers to the safelisted ones (put any token in the body), and parse the body in doPost.

// client
await fetch(WEB_APP_URL, {
  method: 'POST',
  headers: { 'Content-Type': 'text/plain' },
  body: JSON.stringify({ name: 'Ada' }),
});

// Apps Script
function doPost(e) {
  const data = JSON.parse(e.postData.contents);
  return ContentService
    .createTextOutput(JSON.stringify({ ok: true, name: data.name }))
    .setMimeType(ContentService.MimeType.JSON);
}

3Verify

Open the Network tab: there is now a single POST and no OPTIONS request before it.

// in the browser console
await fetch(WEB_APP_URL, { method: 'POST', headers: { 'Content-Type': 'text/plain' }, body: '{"name":"Ada"}' });

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 3

The client does not follow the redirect to script.googleusercontent.com

1Check

For security, content returned by the Content service is redirected to a one-time URL at script.googleusercontent.com, so a client must follow redirects. Compare curl without and with -L.

curl -i -d '{"name":"test"}' -H "Content-Type: text/plain" "$WEB_APP_URL"
curl -L -d '{"name":"test"}' -H "Content-Type: text/plain" "$WEB_APP_URL"

2Fix

Turn on redirect following in your HTTP client (curl uses -L), and check its documentation for the option's name.

curl -L -d '{"name":"test"}' -H "Content-Type: text/plain" "$WEB_APP_URL"

3Verify

The client now returns the JSON your script produced instead of a redirect.

curl -sL -d '{"name":"test"}' -H "Content-Type: text/plain" "$WEB_APP_URL"

Confirmed by developers.google.com

Did this fix it?

How do I stop it coming back?

  • Never use c or sid as parameter names in a web app.
  • Test every new web app deployment with curl -L before wiring a browser client to it.
  • Keep browser calls to a web app as simple requests: a text/plain body and no custom headers, with any token in the body.
  • Give clients the /exec URL of a deployment: the /dev test URL opens only for users with edit access.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

The browser blocks your API response: no Access-Control-Allow-Origin header

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.example.com/orders. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

The browser received the response but refused to hand it to your script, because it did not carry an Access-Control-Allow-Origin header naming the page's origin.

4 causes, each with a fix

Web apps

A Chrome extension content script cannot read another site's response

Access to fetch at 'https://api.example.com/price' from origin 'https://shop.example.net' has been blocked by CORS policy

Since Chrome 85 a content script's request is treated as a request from the web page it runs in, so the page's CORS rules apply and the extension's host permissions do not help.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.