pythonでjsonデータをhttp POSTリクエストするための備忘録
テストサーバ
http://httpbin.org/
を参照させていただきます
A simple HTTP Request & Response Service.
Run locally: $ docker run -p 80:80 kennethreitz/httpbin
実行環境
Python 3.12.3
環境
$ python --version
Python 3.12.3
(recommend) venv
環境を使う
# cd (working directry)
python3 -m venv venv
source venv/bin/activate
# (deactivate)
install
pip install requests
テストコード
コード
https://blog.apify.com/python-post-request/
http_post.pyimport requests import json data = {"name": "Jane Smith", "email": "janesmith@example.com", "job": "Data Scientist"} json_data = json.dumps(data) response = requests.post("http://httpbin.org/post", data=json_data) print(response.text)
実行
$ python http_post.py
{
"args": {},
"data": "{\"name\": \"Jane Smith\", \"email\": \"janesmith@example.com\", \"job\": \"Data Scientist\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "81",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.32.3",
"X-Amzn-Trace-Id": "Root=1-66d3a607-626a1cc315dbbf822e5f9771"
},
"json": {
"email": "janesmith@example.com",
"job": "Data Scientist",
"name": "Jane Smith"
},
"origin": "99.0.85.0",
"url": "http://httpbin.org/post"
}
jsonファイルを引数指定可能にする(-f オプション
)
http_post.py
import requests
import json
from argparse import ArgumentParser
def get_option():
argparser = ArgumentParser()
argparser.add_argument('-f', '--file', type=str,
default="xxx.json",
help='Specify json file')
return argparser.parse_args()
args = get_option()
print('input .json file name: ' + str(args.file))
json_open = open(args.file, 'r')
json_load = json.load(json_open)
data = json_load
#data = {"name": "Jane Smith", "email": "janesmith@example.com", "job": "Data Scientist"}
json_data = json.dumps(data)
response = requests.post("http://httpbin.org/post", data=json_data)
print(response.text)
test.json
{
"name": "Jane Smith",
"email": "janesmith@example.com",
"job": "Data Scientist"
}
実行
$ python http_post.py -f test.json
input .json file name: test.json
{
"args": {},
"data": "{\"name\": \"Jane Smith\", \"email\": \"janesmith@example.com\", \"job\": \"Data Scientist\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "81",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.32.3",
"X-Amzn-Trace-Id": "Root=1-66d3a92e-45c5dea311179eb6539230e8"
},
"json": {
"email": "janesmith@example.com",
"job": "Data Scientist",
"name": "Jane Smith"
},
"origin": "99.0.85.0",
"url": "http://httpbin.org/post"
}
jsonファイル+POST先URLも引数指定可能にする(-f オプション
、-u オプション
)
http_post.py
import requests
import json
from argparse import ArgumentParser
def get_option():
argparser = ArgumentParser()
argparser.add_argument('-f', '--file', type=str,
default="xxx.json",
help='Specify json file')
argparser.add_argument('-u', '--url', type=str,
default="http://example.com",
help='Specify POST URL')
return argparser.parse_args()
# -- arg parser
args = get_option()
JSON_FNAME = args.file
URL = args.url
print('input .json file name: ' + str(JSON_FNAME))
print('input url: ' + str(URL))
# -- http post
json_open = open(args.file, 'r')
json_load = json.load(json_open)
json_data = json.dumps(json_load)
response = requests.post(URL, data=json_data)
print(response.text)
実行
$ python http_post.py -f test.json -u http://httpbin.org/post
input .json file name: test.json
input url: http://httpbin.org/post
{
"args": {},
"data": "{\"name\": \"Jane Smith\", \"email\": \"janesmith@example.com\", \"job\": \"Data Scientist\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "81",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.32.3",
"X-Amzn-Trace-Id": "Root=1-66d3aa6d-55c8c5dc74231dac025dabf6"
},
"json": {
"email": "janesmith@example.com",
"job": "Data Scientist",
"name": "Jane Smith"
},
"origin": "99.0.85.0",
"url": "http://httpbin.org/post"
}
参考