- 新しい順
- 投票が多い順
- コメントが多い順
Are you testing this in the browser or in a different client, such as Postman? Something that usually gets me in these situations is that if you're explicitly adding the CORS headers to the response, the headers aren't added to the response if the function fails before reaching the return statement. Note that CORS is a security feature built in to browsers, so test the API with Postman, CURL or some other client to check if it is a CORS error or not.
Another tip is to ensure you have an understanding of your functions' failure modes, so that you can log errors for debugging and fail gracefully.
So, I don't know your exact architecture but;
-
If you will serve the API and front-end on the same domain in production, and have this error while working local React app:
- You do not need to enable CORS on the API GW.
- Simply you can use your relative API path in the React as '/getUserProfile' instead of 'https://api.linkanimage.com/getUserProfile' and put proxy:'https://api.linkanimage.com' in the webpack config file. This will proxy everything and you will have no CORS issues.
-
If you want to serve the API and front end under different domain names, then:
- You will be doing cross-site requests so, you have to enable CORS in API GW for every method
- You can enable cors with default values and if you want you can limit the Access-Control-Allow-Origin to the necessary domains for more strict policies.
PS.1 Regarding file upload, your content-type should be "multipart/form-data" not "text/plain"
PS.2 Again, if you are using JSON for your regular (not upload) API calls, your content-type will be "application/json"
関連するコンテンツ
- AWS公式更新しました 2年前
Things seem to work with postman - this appears to be a cors issue - it only appears in the browser
If you know that you're attaching the right CORS headers, it probably means that your Lambda function code fails before reaching the return statement, leading to a 5xx response being returned, which doesn't contain the right CORS headers.