requests.post参数中json vs data
2018-12-26 本文已影响21人
好小葱1
在源码中的prepare_body
方法中,如果发现json
参数,会默认为hearders
为Content-Type:application/json
.
def prepare_body(self, data, files, json=None):
"""Prepares the given HTTP body data."""
# Check if file, fo, generator, iterator.
# If not, run through normal process.
# Nottin' on you.
body = None
content_type = None
if not data and json is not None:
# urllib3 requires a bytes-like body. Python 2's json.dumps
# provides this natively, but Python 3 gives a Unicode string.
content_type = 'application/json'
body = complexjson.dumps(json)
if not isinstance(body, bytes):
body = body.encode('utf-8')
...
(调用同事的api时,发现了这个问题,记录一下,省去json.dumps(params)的步骤)