1

I created a microservice in Go that unmarshals the data off of a request body and stores it in DynamoDB. The REST URL runs pretty much anywhere but a browser it seems. When I run the fetch in a browser I get a 403 with no response. I can get a 200 response using a Node script, curl command, or even when using a tool like repl.it to test. For some reason though, it never wants to work in a web app, or the dev console (I'm using Chrome). I tried running the request in multiple websites including the web app I'm building to run it in and all of them gave the same 403 no response result.

In API Gateway, Authorization is set to "none", and API Key is set to "not required", so I'm having a lot of difficulty in seeing how I can get a 403 on my endpoint. On the frontend, my fetch request looks like something below. The curl command that worked is below as well. I followed this post before creating this, and although it's similar, this person does actually get a response, while mine gives me no response. It works as a script, but just doesn't work in the browser. How does this work anywhere but inside a browser? If I can just get some info on what's going on, that would be awesome. If I can get some info on how this can be solved, that would be REALLY awesome.

js

fetch(url, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    data: JSON.stringify({
        Email: 'example@test.com'
    }),
})
    .then(res => res.json())
    .then(json => console.log(json))
    .catch(e => console.error(e))

curl

curl -H 'application/json' -d '{ "Email": "example@test.com" }' $URL

UPDATE: I discovered what I had going wrong. It was a CORS issue. I was not handling the OPTIONS method that was sent as a preflight request prior to my POST request. The OPTIONS method gets sent automatically when in the browser, but does not if you're running the code anywhere else.

DougCal
  • 105
  • 1
  • 1
  • 11
  • 1
    In the Node.JS example you're using `POST` to provide a request body to the endpoint. IIUC when you say "in the browser", I assume you're trying to open the URL in the browser address bar which corresponds to a `GET`. You don't include your `curl` example. Are you using `curl --request POST --header ... --data ..`? If so, that would corroborate my assumption. If this is correct, you can't `POST` from the address bar of a browser and would need to add a `GET` method (wrapper) to your code. – DazWilkin May 05 '19 at 20:21
  • 1
    @DazWilkin I'm running that small Node.JS example inside a React application I built. So, that's what I mean by "in the browser" I guess you can say. There's no query strings or anything like that, just a body that's being sent. And yes, that curl command is very similar. I think it's a good idea to add that to my question in case someone else has the same thought as you. Does this make sense? – DougCal May 05 '19 at 23:09
  • 3
    Clutching at straws: is the domain that's originating the request, different to the domain where your Go server is running? Perhaps you're hitting a CORS issue. – DazWilkin May 06 '19 at 00:48
  • The endpoint on browser is the same with in CURL? – hoangdv May 06 '19 at 01:52
  • 1
    @hoangdv Yes it is – DougCal May 06 '19 at 01:53
  • @JohnCalhoun Check again in Network tab of Chrome dev tool. – hoangdv May 06 '19 at 01:56
  • 1
    @hoangdv I double checked it for you. They are definitely the same. – DougCal May 06 '19 at 01:59
  • 1
    @DazWilkin It is. With my fetch request above, do you have a recommendation on how to see if it is? I'm not sure where I can see "CORS" settings in API Gateway, for I only see a way to enable them which has me believing they are not enabled. – DougCal May 06 '19 at 01:59
  • @JohnCalhoun Poor you! It comes from another reason. – hoangdv May 06 '19 at 02:00
  • 1
    You had a good lead there @DazWilkin – DougCal May 07 '19 at 13:54
  • Were you able to resolve the issue? – DazWilkin May 07 '19 at 15:00
  • 1
    @DazWilkin Yeah, I threw an update in the description describing what happened. – DougCal May 07 '19 at 19:15
  • Nice! I think that warrants being your answer to your question. – DazWilkin May 07 '19 at 19:22

0 Answers0