0

The code that I included to call a API in AWS lambda is given below. urlilb3 python library is uploaded as a zip folder successfully. But when I try to access the particular intent it shows

When I included the API call in AWS lambda (python 3.6), I got

"The remote endpoint could not be called, or the response it returned was invalid" .

Why is it so? What are the prerequisites to be done before including the API calls in python 3.6. I used urllib3 python library and upload as zip folder.?? Is any other things required to do??

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)) 
3
  • 2
    Possible duplicate of Access web within AWS Lambda Commented Aug 21, 2017 at 13:17
  • Did you place your Lambda function inside a VPC without a NAT gateway? Commented Aug 21, 2017 at 13:32
  • I have not placed my lambda function inside a VPC Commented Aug 21, 2017 at 13:33

3 Answers 3

2

Scenario : To obtain weather using an third party 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
Sign up to request clarification or add additional context in comments.

1 Comment

This method can be called inside AWS lambda. I have tried this by mysef.
0

Try printing response.data so you can see it in the logs. That might give you a clue. I would also try to switch to Python Requests instead of URLLib3. You may also need to set the Content Type depending on the implementation of the API you're calling.

Comments

0
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)
     }
    }
   }

3 Comments

While this code may answer the question, it would be better to include some context, explaining how it works and when to use it. Code-only answers are not useful in the long run.
@daisyinfocuit... The code that I was given was working fine but I got the error "The remote endpoint could not be called, or the response it returned was invalid". because of the different module, while analysing each and every section finally I got the error.
I have posted the weather API call section which I have used in my code as an answer. So you can simply call this function inside AWS Lambda

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.