美文网首页
requests.post参数中json vs data

requests.post参数中json vs data

作者: 好小葱1 | 来源:发表于2018-12-26 16:59 被阅读21次

    在源码中的prepare_body方法中,如果发现json参数,会默认为heardersContent-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)的步骤)

    相关文章

      网友评论

          本文标题:requests.post参数中json vs data

          本文链接:https://www.haomeiwen.com/subject/wetklqtx.html