JavaScript Fetch API blocked by CORS policy due to gzip?
See original GitHub issueI 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:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks for looking into this, I’ll take a look at the PR shortly.
@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.