LoginSignup
35
25

More than 3 years have passed since last update.

Python3 requests 使い方メモ

Last updated at Posted at 2019-09-05

基本的な使い方のメモです。

requestsモジュールのインストール

$ pip3 install requests

基本的な書き方(get)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests
import json

# エンドポイント
url = 'https://XXXX-api/vX/XXXX'

# リクエスト
res = requests.get(url)

values = json.loads(res.text)
print(values)

postの場合

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests
import json

# エンドポイント
url = 'https://XXXX-api/vX/XXXX'

data = {
    'id': '12345',
    'version': 'XXXXX',
    'item': 'XXXXX'
    }

# リクエスト
res = requests.post(url, json=data)
values = json.loads(res.text)

print(values)

その他のHTTPメソッド

res = requests.put(url)
res = requests.delete(url)
res = requests.head(url)
res = requests.options(url)

カスタムヘッダーを使う場合

headers = {
    'Authorization': 'Bearer XXXX'
}

res = requests.get(url, headers=headers)

参考

requests クイックスタート
https://requests-docs-ja.readthedocs.io/en/latest/user/quickstart/

35
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
35
25