JSON problem with Python-requests post data

Source: Internet
Author: User
Tags urlencode

The code is as follows:

Import JSON Import  = requests.post (url, data = {"a": Json.dumps ({"b" ) " C " # url is random URL

Use Fiddler to grab the package, although in WebForms you see the format as follows:

But see the results in raw as follows:

That is, automatic execution of the UrlEncode, view requests/models.py source code discovery, if the data can be iterated, that is, automatically join the "application/x-www-form-urlencoded" header, and perform UrlEncode:

In the Prepare_body method of the Preparedrequest class

if Data:     = self._encode_params (data)    ifor'read'):        = None    Else:        'application/ x-www-form-urlencoded'

In the _encode_params (data) method of the Requestencodingmixin class

ifisinstance (data, (str, bytes)):returnDataelifHasattr (data,'Read'):    returnDataelifHasattr (data,'__iter__'): Result= []     forK, vsinchto_key_val_list (data):ifIsinstance (VS, basestring)or  notHasattr (VS,'__iter__'): vs=[vs] forVinchvs:ifV is  notNone:result.append ((K.encode ('Utf-8')ifIsinstance (k, str)ElseK, V.encode ('Utf-8')ifIsinstance (V, str)Elsev))returnUrlEncode (Result, doseq=True)Else:    returnData

Workaround:

Import JSON Import  = {"content-type""application/x-www-form-urlencoded"   "a=%s"% Json.dumps ({"B"    " C "  = requests.post (URL, data = data, headers = headers);

Python-requests JSON problem with post data

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.