0

I'm doing an API with aws lambda and API gateway, but when I make a request with fetch it returns nothing and gives me "No 'Access-Control-Allow-Origin' header is present on the requested resource" message

I tried to use curl and other website to do this request and the answer was

{"statusCode":200,"headers":{"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"POST"},"body":"oi"}

And that is what I expected, but using fetch in a js code it returns me the error: "No 'Access-Control-Allow-Origin' header is present on the requested resource"

And Access-Control-Allow-Origin is already in header of response

The lambda code:

    callback(null, {
        statusCode: 200,
        headers: {
            "Access-Control-Allow-Origin":"*",
            "Access-Control-Allow-Methods":"POST"
        },
        body: "oi"
    })
}

The fetch code:

fetch('URL', {
    method: 'POST',
})
.then((oi) => oi.json())
.then((json) => console.log(json))
  • 1
    Possible duplicate of [Why does my JavaScript code get a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error when Postman does not?](https://stackoverflow.com/questions/20035101/why-does-my-javascript-code-get-a-no-access-control-allow-origin-header-is-pr) – Dan Starns Oct 10 '19 at 19:10
  • I don't think it's a duplicate because i already have Access-Control-Allow-Origin: * in my response header – Paulo Mesquita Oct 10 '19 at 19:20
  • Possible duplicate https://stackoverflow.com/questions/35190615/api-gateway-cors-no-access-control-allow-origin-header – BFunk Oct 10 '19 at 20:05
  • I tried that solutions and nothing worked, and with curl works, so I think maybe is a problem with the way I did my fetch request, but I don't know what is it – Paulo Mesquita Oct 11 '19 at 03:51

2 Answers2

1

I solve the problem going in aws console in API gateway and setting the headers there instead of just in the code

  • Please, could you check it as the preferred answer in order for others to know it is already solved? – Ictus Jun 29 '20 at 04:06
0

Without more information this is hard to diagnose, but I would assume you need to enable CORS on your AWS lambda function. Without seeing some of the lambda code its hard to give you any help.

BFunk
  • 387
  • 5
  • 11
  • I did just a test code `exports.handler = function(event, context, callback){ callback(null, { statusCode: 200, headers: { "Access-Control-Allow-Origin":"*", "Access-Control-Allow-Methods":"POST" }, body: "oi" }) }` – Paulo Mesquita Oct 10 '19 at 19:21
  • What is the trigger for your lambda? – BFunk Oct 10 '19 at 19:31
  • I use API gateway to call it with a request – Paulo Mesquita Oct 10 '19 at 19:34
  • Follow this documentation to make sure that CORS is allowed on the API Gateway https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html – BFunk Oct 10 '19 at 19:35
  • I did this all, enable cors in API gateway and put the headers in the response the problem of this is when I use a sniffer to see the return, the function do not return me nothing – Paulo Mesquita Oct 10 '19 at 19:50
  • I'm not sure what you mean by sniffer. Have you tried the same request using curl or an app like Postman? – BFunk Oct 10 '19 at 20:01
  • CORS always seems to be a headache. I setup a test lambda and enabled cors and it seems to work for me I did notice that I had to enable CORS on the resource itself. not just the method. Also, your lambda is allowing CORS on a POST request but your fetch is making a GET request – BFunk Oct 11 '19 at 04:56