AWS API Gateway CORS for OPTIONS, not working for POST

I looked at other related questions on SO, but that seems different. In fact, my question is very similar to this , except that I have no problem with 400 status.

Setup:

  • Lambda function via api gateway
  • Authorization: No, API KEY Required parameter: false
  • deployment to phase: test

  • 1 resource, 1 POST method integrating lambda.

  • Calling the POST endpoint directly, for example. with curl always returns 200 (with / without payload, bad payload, etc.), so that differs from the question mentioned.

I used the option "Enable CORS" - I tried to apply this option to both the resource and the POST request (and then deploy the API).

In the GW API, I see the Access-Control-Allow-Origin200 POST response headers listed in the response area of ​​the method.

Result: calling the endpoint from the client code in Chrome, OPTIONS passes, but the POST fails due to the lack of a header Access-Control-Allow-Origin.

In curl: calling OPTIONS

curl -X OPTIONS -H "Access-Control-Request-Method: POST" \
     -H "Access-Control-Request-Headers: Content-Type" \
     -H "Origin: http://example.com" --verbose <endpoint>

answer:

< HTTP/1.1 200 OK
< Content-Type: application/json
...
< Access-Control-Allow-Headers: Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token
< Access-Control-Allow-Methods: POST,OPTIONS
< Access-Control-Allow-Origin: *
...

but with POST:

curl -X POST -d '{}' -H "Content-Type: application/json" \
     -H "Origin: http://example.com" --verbose <endpoint>

it returns:

< HTTP/1.1 200 OK
< Content-Type: application/json
...

and json response body - but no Access-header.

What else can I check?

+4
source share
2 answers

The problem was that the API gateway called my lambda function using the option "Lambda Proxy Integration".

I believe this is activated by default when you add an API gateway trigger to a newly created lambda function.

API- - Resource-Method, "Integration Response" , , ( Enable CORS) Access-Control-Allow-Origin, , @Abhigna_Nagaraja .

: "Lambda Proxy Integration", 'Access-Control-Allow-Origin': '*' -.

: - , " Lambda Proxy" CORS ( ).

( json { statusCode, headers, body }.)

, , http json:

http vs json status

json

+12

" CORS" - , /. " CORS", , . " CORS",

  • " --" POST
  • "Access-Control-Allow-Origin" POST

, API curl.

+2

Source: https://habr.com/ru/post/1658302/


All Articles