dtjwov4984 2019-05-05 19:40
浏览 34

在浏览器中调用Go Lambda函数时如何获得响应?

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 模型在y分布之外的数据上预测能力不好如何解决
    • ¥15 processing提取音乐节奏
    • ¥15 gg加速器加速游戏时,提示不是x86架构
    • ¥15 python按要求编写程序
    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
    • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
    • ¥15 opencv图像处理,需要四个处理结果图
    • ¥15 无线移动边缘计算系统中的系统模型
    • ¥15 深度学习中的画图问题
    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条