question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JavaScript Fetch API blocked by CORS policy due to gzip?

See original GitHub issue

I am running into a CORS error and can not seem to find out why. After testing I think it has something to do with either CORS, headers, app.api.binary_types.append("application/json"), or a combination. Can someone help? I have searched the web for a couple of hours, but can not seem to find the answer.

My Chalice app code:

from chalice import Chalice, Response
app = Chalice(app_name="myChaliceApp")
app.api.binary_types.append("application/json")
app.debug = True


@app.route("/some_test3", methods=["POST"], cors=True)
def some_test3():
    request = app.current_request
    data = request.json_body
    blob = json.dumps(data).encode("utf-8")
    payload = gzip.compress(blob)
    custom_headers = {"Content-Type": "application/json", "Content-Encoding": "gzip"}

    return Response(body=payload, status_code=200, headers=custom_headers)

My JavaScript code:

    const url = "https://xxxx.execute-api.eu-central-1.amazonaws.com/api/some_test3"

    const data = {
      Test: 3
    };

    fetch(url, {
        method: 'POST',
        body: JSON.stringify(data),
        headers: {
          'Content-Type': 'application/json',
          'Accept-Encoding': 'gzip'
        }
      })
      .then((response) => response.json())
      .then((data) => {
        console.log('Success:', data);
      })
      .catch((error) => {
        console.error('Error:', error);
      });

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jameslscommented, Feb 25, 2020

Thanks for looking into this, I’ll take a look at the PR shortly.

1reaction
Misza13commented, Feb 24, 2020

@MartijnHarmenzon I think I got to understand the issue, lead by a fix mentioned in https://forums.aws.amazon.com/message.jspa?messageID=785296#785356 - created a simple PR that fixes the OPTIONS integrations created by Chalice. I have already managed to get my API to work the way I intended based on my fork.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript Fetch API Cors : Doesen't pass access control check
The Access-Control-Allow-Origin header is not configured properly by the server. Could you provide the exact Access-Control-Allow-Origin header ...
Read more >
Fetch: Cross-Origin Requests - The Modern JavaScript Tutorial
CORS for safe requests · It ensures that the correct Origin is sent with a cross-origin request. · It checks for permitting Access-Control-Allow- ......
Read more >
Fixing Common Problems with CORS and JavaScript
Tutorial: This post walks through troubleshooting and fixing common problems associated with calling REST APIs from JavaScript.
Read more >
Cross-Origin Resource Sharing (CORS) - MDN Web Docs
The CORS mechanism supports secure cross-origin requests and data transfers between browsers and servers. Modern browsers use CORS in APIs such ...
Read more >
Resolving CORS Policy Error When Custom Headers Are ...
To resolve this error make sure the "Header" field in the CORS policy is updated with all the headers in a comma-separated manner....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found