我有用于将帖子发送到AWS API网关的代码
payload = {"thing-name": thing_name, "thing-name-sig": sig}
r = requests.post(api_gw, data=payload)
api网关返回:方法已完成,状态:415
用于未知媒体
当我将python从2.7升级到3.6时问题开始了>
代码以这种方式工作:
payload = {"thing-name": thing_name, "thing-name-sig": sig} r = requests.post(api_gw, data=json.dumps(payload))
它正常工作,但是一旦我升级到3.6,python编译器就开始给我这个错误:
TypeError:“字节”类型的对象不可JSON序列化
所以我必须将其更改为data=payload
以使其通过python编译器,但随后它无法通过api网关,并返回状态为415的已知媒体错误。
所以有两种解决方案:
1-是我如何找到一种在python 3.6中而不是dict JSON可序列化对象发送JSON字符串数据格式的方法。我不能。
2-某些如何使aws服务的api网关能够接受python request.post发送的dict JSON可序列化对象。这也做不到:(
这里有任何帮助。
我尝试过:
payload = {"thing-name": thing_name, "thing-name-sig": sig} r = requests.post(api_gw, json=payload)
它返回了该故障:
Traceback (most recent call last):
File "emon-device.py", line 154, in <module>
r = requests.post(api_gw, json=payload)
File "/home/ec2-user/.local/lib/python3.6/site-packages/requests/api.py", line 116, in post
return request('post', url, data=data, json=json, **kwargs)
File "/home/ec2-user/.local/lib/python3.6/site-packages/requests/api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "/home/ec2-user/.local/lib/python3.6/site-packages/requests/sessions.py", line 519, in request
prep = self.prepare_request(req)
File "/home/ec2-user/.local/lib/python3.6/site-packages/requests/sessions.py", line 462, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/home/ec2-user/.local/lib/python3.6/site-packages/requests/models.py", l ine 316, in prepare
self.prepare_body(data, files, json)
File "/home/ec2-user/.local/lib/python3.6/site-packages/requests/models.py", l ine 466, in prepare_body
body = complexjson.dumps(json)
File "/usr/lib64/python3.6/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib64/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib64/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/lib64/python3.6/json/encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'bytes' is not JSON serializable
[我有用于将帖子发送到AWS API网关有效负载的代码= {“ thing-name”:事物名称,“ thing-name-sig”:sig} r = request.post(api_gw,data = payload) api网关返回:方法...
尝试一下: