Hit the web API in Python

** How to access HTTP with Python **

This time, we will use the Requests library to handle the REST-style Web API.

HTTP method

Before we use it, let's take a brief look at HTTP methods / REST APIs. The type of request made from the client to the server is called a method, and there are basically the following eight types.

Method Description
GET Get resources
HEAD Get only HTTP headers for resources
POST Send data from client to server
PUT Save resources
DELETE Delete resource
CONNECT Establish a tunnel with the server
OPTIONS Find out which methods the server allows
TRACE Examine the network route to the server

REST API is an API implementation that uses four HTTP methods "GET", "POST", "PUT", and "DELETE".

HTTP method Behavior in REST
GET Resource acquisition (READ)
POST Resource creation (CREATE)
PUT Resource update (UPDATE)
DELETE Delete resource (DELETE)

Requests library installation

Installation is done with pip.

pip install requests

How to use

Basically, it is OK if you specify the URL as follows (parameters are optional)

import requests

url = "http://xxxxx"
pyaload = {"key1":"value1", "key2":"value2"}

r = requests.get(url, params=payload)

The usage is the same except for GET.

r = requests.post(url)
r = requests.put(url)
r = requests.delete(url)

response

The response from the server can be confirmed as follows according to the response format.

#text
r.text

#binary
r.content

# JSON
r.json()

#Raw response
r.raw

#HTTP status code of the response
r.status_code

Actually use

Try hitting this Zip Code Search API from Python.

import requests

url = "http://zip.cgis.biz/xml/zip.php"
payload = {"zn": "1310045"}

r = requests.get(url, params=payload)

r.text

Execution result

'<?xml version="1.0" encoding="utf-8" ?>\n<ZIP_result>\n<result name="ZipSearchXML" />\n<result version="1.01" />\n<result request_url="http%3A%2F%2Fzip.cgis.biz%2Fxml%2Fzip.php%3Fzn%3D1310045" />\n<result request_zip_num="1310045" />\n<result request_zip_version="none" />\n<result result_code="1" />\n<result result_zip_num="1310045" />\n<result result_zip_version="0" />\n<result result_values_count="1" />\n<ADDRESS_value>\n<value state_kana="Tokyo" />\n<value city_kana="Sumidaku" />\n<value address_kana="Ossiage" />\n<value company_kana="none" />\n<value state="Tokyo" />\n<value city="Sumida Ward" />\n<value address="Oshiage" />\n<value company="none" />\n</ADDRESS_value>\n</ZIP_result>\n'

Referenced site

Recommended Posts

Hit the web API in Python
Hit Mastodon's API in Python
Hit the Firebase Dynamic Links API in Python
Getting the arXiv API in Python
[Python] Hit the Google Translation API
Hit the Etherpad-lite API with Python
Access the Twitter API in Python
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Tweet using the Twitter API in Python
Try hitting the YouTube API in Python
Evernote API in Python
C API in Python 3
An easy way to hit the Amazon Product API in Python
Hit the New Relic API in Python to get the server status
Hit the Web API using requests Example: Flickr
Try using the BitFlyer Ligntning API in Python
Tips for hitting the ATND API in Python
Try using the DropBox Core API in Python
Hit a method of a class instance with the Python Bottle Web API
Web scraping notes in python3
Download the file in Python
EXE Web API by Python
Web API with Python + Falcon
Blender Python API in Houdini (Python 3)
Call the API with python3.
Initial settings when using the foursquare API in python
PHP and Python samples that hit the ChatWork API
Using the National Diet Library Search API in Python
python web scraping-get elements in bulk
Test.py is not reflected on the web server in Python3.
Hit a command in Python (Windows)
Get your heart rate from the fitbit API in Python!
Python in the browser: Brython's recommendation
Save the binary file in Python
Web application development memo in python
Get the desktop path in Python
Get the script path in Python
In the python command python points to python3.8
Implement the Singleton pattern in Python
Use the Flickr API from Python
I wrote the queue in Python
Calculate the previous month in Python
Examine the object's class in python
Get the desktop path in Python
Quickly implement REST API in Python
Get the host name in Python
web coder tried excel in Python
The first step in Python Matplotlib
I wrote the stack in Python
Master the weakref module in Python
[Python / Django] Create a web API that responds in JSON format
Regularly upload files to Google Drive using the Google Drive API in Python
Python beginners hit the unofficial API of Google Play Music to play music
Play by hitting the Riot Games API in Python First half
Learn the design pattern "Prototype" in Python
Learn the design pattern "Builder" in Python
Mouse operation using Windows API in Python
[Python] Web application from 0! Hands-on (3) -API implementation-
Load the remote Python SDK in IntelliJ
Start the web server in the current directory