テキストファイルにpayloadを設定してpost送信
テキストファイルを準備する(urlの末尾、payload)
update-employee,{"user_id":"9991","name_sei":"山田","name_mei":"花子"}
update-employee,{"user_id":"9992","name_sei":"山田","name_mei":"太郎"}
payloadの送信
try:
datalist = []
txtfile = r'<post.txtのフルパス>'
with open(txtfile, 'r', encoding="utf-8") as f:
while True:
data = f.readline()
if not data:
break
dlist = data.split(',')
datalist.append((dlist[0], data.replace(
dlist[0] + ',', '').replace('\n', '')))
urlbase = '<エンドポイント(末尾の手前まで)>'
headers = {
'Content-Type': 'application/json'
}
for data in datalist:
url = urlbase + data[0]
payload = data[1]
req = urllib.request.Request(
url, payload.encode(), headers, 'POST')
with urllib.request.urlopen(req) as res:
body = res.read()
except Exception as e:
print(e)