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)