【发布时间】:2022-09-23 05:04:25
【问题描述】:
我试图运行美国邮政服务的网络工具, 用于将邮政编码转换为州和城市。我在 AWS Amplify 中创建了一个 AWS Lambda 函数。
但是 Lambda 函数总是给我返回消息{\"message\":\"Internal Server Error\"}
这是我的 Lambda 函数代码。
const axios = require(\"axios\");
const BASE_URI =
\"http://production.shippingapis.com/ShippingAPITest.dll?API=CityStateLookup&XML=\";
const config = {
headers: {
\"Content-Type\": \"text/xml\",
\"Access-Control-Allow-Origin\": \"*\",
\"Access-Control-Allow-Credentials\": true,
\"Access-Control-Allow-Methods\": \"GET\",
},
method: \"get\",
};
exports.handler = async function (event, context, callback) {
// The zipcode is sent by the frontend application.
// This is where we use it.
const zipcode = event.queryStringParameters.zipcode;
// The xml variable is the string we are going to send to the
// USPS to request the information
const xml = `<CityStateLookupRequest USERID=\"400000000\"><ZipCode ID=\"0\"><Zip5>${zipcode}</Zip5></ZipCode></CityStateLookupRequest>`;
try {
// Using syntactic sugar (async/await) we send a fetch request
// with all the required information to the USPS.
const response = await axios(`${BASE_URI}${xml}`, config);
// We first check if we got a good response. response.ok is
// saying \"hey backend API, did we receive a good response?\"
if (!response.ok) {
// If we did get a good response we store the response
// object in the variable
return { statusCode: response.status, body: response };
}
// Format the response as text because the USPS response is
// not JSON but XML
const data = await response.text();
// Return the response to the frontend where it will be used.
return {
statusCode: 200,
body: data,
};
// Error checking is very important because if we don\'t get a
// response this is what we will use to troubleshoot problems
} catch (err) {
console.log(\"Error: \", err);
return {
statusCode: 500,
body: JSON.stringify({ msg: err.message }),
};
}
};
我认为axios 工作正常。
任何帮助将不胜感激,因为我正在尝试解决这个问题好几天。
-
您是否检查了 cloudwatch 中的功能日志?
-
\"errorType\": \"Error\", \"errorMessage\": \"无法字符串化响应正文\",
标签: amazon-web-services lambda serverless usps