Connor Dorward
Connor Dorward

Reputation: 37

Getting a 302 response to my microcontroller but not to postman?

I'm sending a get request to my apps script from my microcontroller and its returning a 302 although when I request from postman I get a 200? Does anyone have any idea whats going on here? Here's the response headers and response:

Response Headers:
    Content-Type : text/html; charset=UTF-8
    Access-Control-Allow-Origin : *
    Cache-Control : no-cache, no-store, max-age=0, must-revalidate
    Pragma : no-cache
    Expires : Mon, 01 Jan 1990 00:00:00 GMT
    Date : Tue, 02 Mar 2021 21:04:31 GMT
    Location : https://script.googleusercontent.com/macros/echo?user_content_key=zelsIoIalPSN2brC0Ucwm8KErWsGngFzg3-RfIKKoFhB-TC6uc1StPYE9BZ_6BTOD2biigVTX8niN5E79oBu8ueNSxgDLkcgOJmA1Yb3SEsKFZqtv3DaNYcMrmhZHmUMWojr9NvTBuBLhyHCd5hHa7ei_lA4UjdzA0HOq-9axFFfVZ9KGEnuHPSmwni1hyk6iDJynKJ3CBqbqlbgGEEvpUd579GOljK4AMXDeW9ltFitnlNC15LBrOtvNFeZBCKPCRWEug3jF7jPBmOj32zQcqoYE01sWW00Xz3hUTq3dXcIZ-nkxRkJc5wKdCNugX5Ze5ERSdO3706zPPNASHCYLA&lib=MWEOgn9XagWcbCIn8zJLe3PqOnSALuAMd
    Content-Security-Policy : script-src 'report-sample' 'nonce-PBuJkJjKxMQRpM3M1MHVXQ' 'unsafe-inline' 'unsafe-eval' 'strict-dynamic' https: http:;object-src 'none';base-uri 'self';report-uri /cspreport
    X-Content-Type-Options : nosniff
    X-Frame-Options : SAMEORIGIN
    X-XSS-Protection : 1; mode=block
    Server : GSE
    Alt-Svc : h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
    Accept-Ranges : none
    Vary : Accept-Encoding
    Transfer-Encoding : chunked
Response:
<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=zelsIoIalPSN2brC0Ucwm8KErWsGngFzg3-RfIKKoFhB-TC6uc1StPYE9BZ_6BTOD2biigVTX8niN5E79oBu8ueNSxgDLkcgOJmA1Yb3SEsKFZqtv3DaNYcMrmhZHmUMWojr9NvTBuBLhyHCd5hHa7ei_lA4UjdzA0HOq-9axFFfVZ9KGEnuHPSmwni1hyk6iDJynKJ3CBqbqlbgGEEvpUd579GOljK4AMXDeW9ltFitnlNC15LBrOtvNFeZBCKPCRWEug3jF7jPBmOj32zQcqoYE01sWW00Xz3hUTq3dXcIZ-nkxRkJc5wKdCNugX5Ze5ERSdO3706zPPNASHCYLA&amp;lib=MWEOgn9XagWcbCIn8zJLe3PqOnSALuAMd">here</A>.
</BODY>
</HTML>

Upvotes: 0

Views: 1628

Answers (3)

Eduardo Matsuoka
Eduardo Matsuoka

Reputation: 716

For anyone having problems when a 302 response is being returned, if you just want the response code 200 and no payloads, there is an easy fix:

As stated in another answer, using ContentService on the return statement will result in HTTP 302 that will redirect to another address, and this will return a HTTP 200.

By returning HtmlService.createHtmlOutput(), it will return a 200 response, with no redirections. This made Apps Script doPost work as a webhook destination.

Upvotes: 0

TheAddonDepot
TheAddonDepot

Reputation: 8964

GAS Web Apps redirect, hence the response code 302. Postman allow redirects and resolve to 200.

Here's a quote from the Apps Script reference documentation for the TextOutput class explaining the situation:

Due to security considerations, scripts cannot directly return text content to a browser. Instead, the browser is redirected to googleusercontent.com, which will display it without any further sanitization or manipulation.

Upvotes: 2

romkey
romkey

Reputation: 7069

302 is a "temporarily moved" redirect.

Postman is processing it for you automatically. The ESP32 is not a very powerful processor; you need to handle it yourself in this environment.

The new location is in the Location field in the response header. You'll need extract the location and then do another request to it.

Upvotes: 2

Related Questions