【发布时间】:2021-01-30 05:27:41
【问题描述】:
我正在创建一个 API 来向 DynamoDB 中的表发出 GET 和 POST 请求。
我使用无服务器部署它并接收每种 API 类型的端点。
但是在使用 Postman 进行测试时,出现以下错误:
Bad request. We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
在表中创建数据的代码:
const postsTable = process.env.POSTS_TABLE;
// Create a response
function response(statusCode, message) {
return {
statusCode: statusCode,
body: JSON.stringify(message)
};
}
// Create a post
module.exports.createPost = (event, context, callback) => {
const reqBody = JSON.parse(event.body);
if (
!reqBody.title ||
reqBody.title.trim() === "" ||
!reqBody.body ||
reqBody.body.trim() === ""
) {
return callback(
null,
response(400, {
error:
"Post must have a title and body and they must not be empty"
})
);
}
const post = {
id: uuidv4(),
createdAt: new Date().toISOString(),
userId: 1,
title: reqBody.title,
body: reqBody.body
};
return db
.put({
TableName: postsTable,
Item: post
})
.promise()
.then(() => {
callback(null, response(201, post));
})
.catch(err => response(null, response(err.statusCode, err)));
};
【问题讨论】:
-
错误请求是指状态码 400 吗?可能只是您没有正确调用您的 API。错误代码列表 -> w3.org/Protocols/rfc2616/rfc2616-sec10.html
-
实际上是 403。
-
确保调用正确的 url。
-
您能否发布部署命令的输出、serverless.yml 以及您如何在 postman 中调用 API?
-
设法解决了它并且没有使用无服务器。问题是关于角色和政策以及我发布的参数。
标签: amazon-web-services aws-lambda amazon-dynamodb amazon-cloudfront serverless-framework