Python で学ぶ API 入門
Chris Ramakers
Engineering Manager


1XX: 情報レスポンス2XX: 成功レスポンス3XX: リダイレクト4XX: クライアントエラー5XX: サーバーエラー200: OK404: Not Found500: Internal Server Error
key1: 値 1
key2: 値 2

accept: application/json ヘッダーを追加content-type: application/json ヘッダーで応答# リクエストにヘッダーを追加
response = requests.get(
'https://api.datacamp.com',
headers={'accept':'application/json'}
)
# レスポンスヘッダーを読む
response.headers['content-type']
'application/json'
response.headers.get('content-type')
'application/json'
# ステータスコードにアクセス response = requests.get('https://api.datacamp.com/users/12')response.status_code == 200
True
# requests.codes でステータスコードを参照 response = requests.get('https://api.datacamp.com/this/is/the/wrong/path')response.status_code == requests.codes.not_found
True
Python で学ぶ API 入門