python - TypeError: b'1' is not JSON serializable -
i trying send post request json.
*email variable of type "bytes"
def request_to_send(email, index): url = "....." info = { "body": email.decode('utf-8'), "query_id": index, "debug": 1, "client_id": "1", "campaign_id": 1, "meta": {"content_type": "mime"} } headers = {'content-type': 'application/json'} try: response = requests.post(url, data=json.dumps(data), headers=headers) except requests.connectionerror: sys.exit() homecoming response i error:
file "c:\python34\lib\json\encoder.py", line 173, in default raise typeerror(repr(o) + " not json serializable") typeerror: b'1' not json serializable could please tell me doing wrong?
this happening because you're passing bytes object in data dict (b'1', specifically), value of index. need decode str object before json.dumps can work it:
data = { "body": email.decode('utf-8'), "query_id": index.decode('utf-8'), # decode here "debug": 1, "client_id": "1", "campaign_id": 1, "meta": {"content_type": "mime"} } python json python-3.x http-post
No comments:
Post a Comment