可以通过API网关获取但不能发布到Lambda.为什么? [英] Can GET but not POST to a Lambda via API Gateway. Why?

查看:121
本文介绍了可以通过API网关获取但不能发布到Lambda.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是客户端发出的请求. GET请求成功,但是POST请求给出了错误.

Below is the request the client makes. The GET request is a success, but the POST request gives an error.

fetch('api/public/libraries/sign-out-discourse', {
    method: 'GET', // or 'POST'
    headers: new Headers([
        ['Accept', 'application/json'],
        ['Content-Type', 'application/json'],
        ['Authorization', jwtToken],
    ]),
})

这是POST请求中的错误:

Here is the error from the POST request:

HTTP/1.1 403 Forbidden
Server: CloudFront
Date: Fri, 05 Oct 2018 08:50:14 GMT
Content-Type: text/html
Content-Length: 694
Connection: keep-alive
X-Cache: Error from cloudfront
Via: redacted
X-Amz-Cf-Id: redacted

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>403 ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
This distribution is not configured to allow the HTTP request method that was used for this request. The distribution supports only cachable requests.

<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: redacted
</PRE>
<ADDRESS>
</ADDRESS>
</BODY></HTML>

我尝试过的事情

在CloudFront中,我选择了行为->编辑->允许的HTTP方法:GET,HEAD,OPTIONS,PUT,POST,PATCH,DELETE.自从有机会在AWS以某种方式将设置存储在CloudFront缓存中的情况下获得机会以来,我已经使缓存无效.

In CloudFront I have chosen behaviors -> edit -> Allowed HTTP Methods: GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE. I have invalidated caches since making this chance in the unlikely case that AWS stores settings in the CloudFront cache somehow.

在API Gateway图形中,我为资源启用了CORS.自进行此更改以来,我已经重新部署了API.

In API Gateway graphical, I have enabled CORS for the resource. I have redeployed the API since making this change.

其他调试

curl -X POST API_ENDPOINT返回与从我的应用程序中通过POST调用端点相同的错误.

curl -X POST API_ENDPOINT returns the same error as calling the endpoint with POST from my application.

推荐答案

如果您使用的是Lambda代理集成,则需要在响应中提供CORS标头,对于POST请求,API Gateway不会为您这样做./p>

If you are using Lambda Proxy integration, you need to provide the CORS header in the response, API Gateway will not do that for you for a POST request.

var response = {
    statusCode: 200,
    headers: {
        "Access-Control-Allow-Origin" : "*",
        "Access-Control-Allow-Credentials" : true
    },
    body: JSON.stringify({"message":"Success"})
}
callback(null, response);

这篇关于可以通过API网关获取但不能发布到Lambda.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆