pythonでPOST リクエストを使って、下記のAPI経由で文字を入力したいです。
※APIのURLテストです。
ただ日本語で行うと下記のエラーが表示されます。
エンコードの問題でしょうか。
エラー内容
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 42-46: Body ('【テスト】') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.
英語だと問題なく行きます。
content='%E3%83%86%E3%82%B9%E3%83%88'の文字にすると
テストの内容の文字を入力できます。
文字コードをどのように変換すれば良いか。
教えていただけると幸いでです。
コード
import requests #payload操作 content="テスト".encode() print(content.decode('utf-8')) url = "http://test/api/v1/comment_test/" payload='petition_id=1111&petition_type=1&content='+content.decode('utf-8')+'&m_user_id=111&parent=&test=0' response = requests.request("POST", url, headers=headers, data=payload)
お手数ですが、よろしくお願い致します。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/12/06 07:49