【问题标题】:How to call an external API or URL ( python code) in AWS lambda function?如何在 AWS lambda 函数中调用外部 API 或 URL(python 代码)?
【发布时间】:2018-05-09 16:58:35
【问题描述】:

下面给出了我在 AWS lambda 中调用 API 的代码。 urlilb3 python 库已成功上传为 zip 文件夹。但是当我尝试访问它显示的特定意图时

当我在 AWS lambda (python 3.6) 中包含 API 调用时,我得到了

“无法调用远程端点,或者它返回的响应无效”。

为什么会这样?在 python 3.6 中包含 API 调用之前需要完成哪些先决条件。我使用了 urllib3 python 库并作为 zip 文件夹上传。??还有什么需要做的吗??

def get_weather(session):
    should_end_session = False
    speech_output = " "
    reprompt_text = ""
    api = "some url ...."
    http = urllib3.PoolManager()
    response = http.request('GET',api)
    weather_status = json.loads(response.data.decode('utf-8'))
    for weather in weather_status:
        final_weather = weather["WeatherText"]
    return build_response(session_attributes, build_speechlet_response(speech_output, reprompt_text, should_end_session)) 

【问题讨论】:

  • 您是否将 Lambda 函数放置在没有 NAT 网关的 VPC 中?
  • 我没有将我的 lambda 函数放在 VPC 中

标签: python amazon-web-services aws-lambda python-3.6


【解决方案1】:

场景:使用第三方API获取天气

 import urllib3

 def get_weather():
    api = "some url ...."
    http = urllib3.PoolManager()
    response = http.request('GET',api)
    weather_status = json.loads(response.data.decode('utf-8'))
    for weather in weather_status:
        final_weather = weather["WeatherText"] ## The attribute "WeatherText" will varies depending upon the weather API you are using.  
    return final_weather 

    get_weather() # simple function call

【讨论】:

  • 这个方法可以在 AWS lambda 内部调用。我自己试过这个。
【解决方案2】:

尝试打印 response.data,以便您可以在日志中看到它。这可能会给你一个线索。我也会尝试切换到 Python Requests 而不是 URLLib3。您可能还需要根据您调用的 API 的实现来设置内容类型。

【讨论】:

    【解决方案3】:
    from __future__ import print_function
    import json
    from botocore.vendored import requests
    def lambda_handler(event, context):
       print('received request: ' + str(event))
       doctor_intent = event['currentIntent']['slots']['doctor']
       email_intent = event['currentIntent']['slots']['email']
       print(doctor_intent, email_intent)
       print(type(doctor_intent), type(email_intent))
       utf8string = doctor_intent.encode("utf-8")
       utf8string1 = email_intent.encode("utf-8")
       print(type(utf8string))
       print(type(utf8string1))
       car1 = {"business_name": utf8string , "customer_email": utf8string1 }  
       r = requests.post('https://postgresheroku.herokuapp.com/update', 
       json=car1)
       #print ("JSON         : ", r.json())
       print(r.json())
       data = str(r.json())
       print(type(data))
       return {
        "dialogAction": {
        "type": "Close",
        "fulfillmentState": "Fulfilled",
        "message": {
            "contentType": "PlainText",
            "content": "Thank you for booking appointment with {doctor} 
    {response}".format(doctor=doctor_intent,response=data)
         }
        }
       }
    

    【讨论】:

    • 虽然这段代码可以回答这个问题,但最好包含一些上下文,解释它是如何工作的以及何时使用它。从长远来看,纯代码的答案没有用处。
    • @daisyinfocuit... 给我的代码工作正常,但我收到错误“无法调用远程端点,或者它返回的响应无效”。由于模块不同,在分析每个部分时最后我得到了错误。
    • 我已经发布了我在代码中使用的天气 API 调用部分作为答案。所以你可以简单地在 AWS Lambda 中调用这个函数
    猜你喜欢
    • 1970-01-01
    • 2020-11-21
    • 2018-08-28
    • 2022-10-23
    • 1970-01-01
    • 2018-08-13
    • 2021-09-19
    • 2020-08-19
    • 2022-01-27
    相关资源
    最近更新 更多