request.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:aquests 作者: hansroh 项目源码 文件源码
def serialize (self):
        if not self.params:
            if self.get_method () in ("POST", "PUT", "PATCH"):
                self.headers ["Content-Length"] = 0
            return b""

        # formdata type can be string, dict, boolean
        if self.get_method () in ("GET", "DELETE"):
            if type (self.params) is dict:
                params = self.urlencode (to_bytes = False)
            else:
                params = self.params
            self.uri += "?" + params
            self.path += "?" + params
            self.params = None
            return b""

        data = self.params
        header_name, content_type = self.get_header ("content-type", True)

        if not content_type:
            content_type = "application/x-www-form-urlencoded"

        if type (self.params) is dict:          
            if content_type.startswith ("application/json"):
                data = json.dumps (self.params).encode ("utf8")
                content_type = "application/json; charset=utf-8"
            elif content_type.startswith ("application/x-www-form-urlencoded"):
                data = self.urlencode ()                
                content_type = "application/x-www-form-urlencoded; charset=utf-8"
            elif content_type.startswith ("text/namevalue"):
                data = self.nvpencode ()                
                content_type = "text/namevalue; charset=utf-8"
        self.headers ["Content-Type"] = content_type
        return self.to_bytes (data)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号