Python request post with JSON array in body

Source: Internet
Author: User

Today by this card for a long time, finally solve the discovery is a small problem, haha

Recording:

Send the POST request with request, originally when the body is normal string and the number of all smooth, today encountered the body inside there is a JSON array, the result is to report parameter error

The workaround is simply to precede the JSON array with json.dumps ([{"A": 1, "B": 2},{json2}]), just like this, be sure to note that the function is to be written outside of the brackets, and today it is because the function is written out of a single JSON

Program:

# Coding:utf-8

Import requests

Import JSON

url = "Https://XXXXX"


headers = {' UserName ': ' XXXXX ',
' Accesstoken ': ' XXXXX '}

data={
' UserName ': ' XXXXX ',
' DeviceId ': ' xxxxxxxxx ',
' appversion ': ' XXXXX '
' Datas ': json.dumps
(
[
{"DeviceId": "XXXXXXX",
"DeviceType": "XXXXX",
"Time": "2017-08-28 10:32:28",
"TimeZone": 2
"Phonetime": "2017-08-28 10:32:28"},
{Json2},
{Json3}
]
)
}

r = Requests.post (URL, headers=headers, data=data)


Print R.json ()

Python request post with JSON array in body

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.