Call the API with python3.

A note that calls the API using python's ʻurllib.request` module.

Preparation

lambda should return the received ʻevent`.

import json

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': event
    }

API Gateway provides various methods (GET, POST, PUT).

Execute [Deploy API] and get the URI. (If you update something with API Gateway, deploy it every time! It will not be reflected unless you do it. ← I'm really into this.)

Select [Create] from [Usage Plan], select the API stage, set the rate burst quota, and create the API key.

Hit the API of the main subject lambda

import json
import urllib.request, urllib.error

request_url = "https://xxxx.execute-api.ap-northeast-1.amazonaws.com/stage"

api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"

def lambda_handler(event, context):
    headers = {'x-api-key': api_key, "Content-Type":"application/json"}

    request_json = {
        "key":"val"
    }
    
    req = urllib.request.Request(url=request_url, method="GET", headers=headers, data=json.dumps(request_json).encode())
    
    with urllib.request.urlopen(req) as res:
        body = res.read().decode()
    
    return json.loads(body)

reference

Corrected the following https://qiita.com/j_tamura/items/5a22b102a58d1fa93a78

Recommended Posts

Call the API with python3.
Hit the Etherpad-lite API with Python
Use Trello API with python
Use Twitter API with Python
Web API with Python + Falcon
Play RocketChat with API / Python
Use subsonic API with python3
Note calling the CUDA Driver API with Python ctypes
I moved the automatic summarization API "summpy" with python3.
I tried hitting the API with echonest's python client
Call C from Python with DragonFFI
Getting the arXiv API in Python
Create Awaitable with Python / C API
Get reviews with python googlemap api
Hit the Sesami API in Python
Run Rotrics DexArm with python API
Specifying the date with the Twitter API
[Python] Hit the Google Translation API
Quine Post with Qiita API (Python)
Get the weather with Python requests
Get the weather with Python requests 2
Install the Python plugin with Netbeans 8.0.2
Output the call graph with PyCallGraph
[python] Read information with Redmine API
Hit the web API in Python
Use the Flickr API from Python
Call python from nim with Nimpy
Master the type with Python [Python 3.9 compatible]
Access the Twitter API in Python
A note about hitting the Facebook API with the Python SDK
The first API to make with python Djnago REST framework
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
Make the Python console covered with UNKO
Collecting information from Twitter with Python (Twitter API)
Access the Docker Remote API with Requests
FizzBuzz with Python3
Scraping with Python
Automatically create Python API documentation with Sphinx
Statistics with python
Try using the Kraken API in Python
Behind the flyer: Using Docker with Python
Simple Slack API client made with Python
Scraping with Python
Retrieving food data with Amazon API (Python)
[September 2020 version] Explains the procedure to use Gmail API with Python
Check the existence of the file with python
I created a Python library to call the LINE WORKS API
[Python] Get the variable name with str
Discord Bot with recording function starting with Python: (5) Directly operate the Discord API
Twilio with Python
Tweet using the Twitter API in Python
Integrate with Python
Search the maze with the python A * algorithm
Play with 2016-Python
AES256 with python
Create REST API that returns the current time with Python3 + Falcon
Working with OpenStack using the Python SDK
python starts with ()
Download files on the web with Python
Call the python debugger at any time
Bingo with python