我试图运行美国邮政服务的网络工具,用于将邮政编码转换为州和城市。我在AWS放大器中创建了一个AWS Lambda函数。但是Lambda函数总是给我返回消息{"message":"Internal Server Error"}
这是我的Lambda FUnction代码。
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运行得很好。
任何帮助都将不胜感激,因为我正试图解决这几天。
发布于 2022-09-22 21:04:25
默认情况下,Lambda函数没有出站访问internet的权限。您可以添加Nat网关到您的VPC,但它并不便宜。
https://stackoverflow.com/questions/71805726
复制相似问题