0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

テキストファイルにpayloadを設定してpost送信

Posted at

テキストファイルに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)
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?