首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AWS Lambda函数:{“message”:“内部服务器错误”

问AWS Lambda函数:{“message”:“内部服务器错误”
EN

Stack Overflow用户
提问于 2022-04-09 06:20:19
回答 1查看 290关注 0票数 0

我试图运行美国邮政服务的网络工具,用于将邮政编码转换为州和城市。我在AWS放大器中创建了一个AWS Lambda函数。但是Lambda函数总是给我返回消息{"message":"Internal Server Error"}

这是我的Lambda FUnction代码。

代码语言:javascript
复制
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运行得很好。

任何帮助都将不胜感激,因为我正试图解决这几天。

EN

回答 1

Stack Overflow用户

发布于 2022-09-22 21:04:25

默认情况下,Lambda函数没有出站访问internet的权限。您可以添加Nat网关到您的VPC,但它并不便宜。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71805726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档