A comparison between Shell curl and Python requests

Source: Internet
Author: User
Shell Curl and Python requests

Stumbled upon a distinction between the curl and the requests library. The scene is like this –

This uses curl to initiate a POST request:

Curl-v-x POST http://api.xx.com/api/yy.php--data ' params={"sign": "XXXX", "data": [{"UID": "Remark": "Just4test"}] }'

The backend interface service can parse the params parameters and respond to the results normally. But when I switch to Python to request:

Import JSON
import requests

data = {"sign": "XXXX", "data": [{"UID": $, "remark": "Just4test"}]}
reqbody = ( ' params=%s '%json.dumps (data)
res = requests.post (
    ' http://api.xx.com/api/yy.php ',
    data=reqbody)

The interface could not parse params and return an error.
Then carefully compared headers and body, found that there may be the header set different reasons.
To look closely, the header in the curl has one row:

content-type:application/x-www-form-urlencoded

However, there is no corresponding setting in the Python requests, so the header is added and the request returns normally.

import JSON import requests = {' headers ': ' Content-type '} data = {"sign": "XXXX", "data": [{"UID": "Remark": "Just4test"}]} Reqbody = (' params=%s '%json.dumps (data)) res = Requests.post (' http://api.xx.com/api/yy.php ', Data=reqbody, headers=headers) 
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.