I am using python requests
module, I send my params like this before:
requests.post(url=url, params=params)
but today, I find that I send my data like this, it fails, I change to this:
requests.post(url=url, data=params)
then it is ok, what is the difference between data
and params
?
I observed that the request got a header X-Requested-With:XMLHttpRequest
, is it because of this?
Parameters are options you can pass with the endpoint (such as specifying the response format or the amount returned) to influence the response. There are several types of parameters: header parameters, path parameters, and query string parameters.
Form the data is posted in http header whereas in QueryString data is sent through url. Request. Form is used for accessing the value on post back of form and Request. QueryString is used for accessing the value passes in url as a parameter.
The parameter data is defined as a pointer to an array of floating-point values via. From: Data Acquisition Techniques Using PCs (Second Edition), 2003.
An HTTP POST request is used to send data to a server, where data are shared via the body of a request. In the request. post() function, data are sent with the data parameter, which accepts a dictionary, a list of tuples, bytes or a file object.
According to the requests documentation:
requests.post(url, data=data)
will make an HTTP POST request, andrequests.get(url, params=params)
will make an HTTP GET request To understand the difference between the two, see this answer.
Here's how params can be used in a GET:
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get('http://httpbin.org/get', params=payload)
print(r.text)
Which outputs
{
"args": {
"key1": "value1",
"key2": "value2"
},
[...]
"url": "http://httpbin.org/get?key1=value1&key2=value2"
}
Notice that the payload ended up in the query string of the URL. Since they ended up there, they are viewable by anyone who has access to the URL, which is why you shouldn't use query strings for sensitive data like passwords.
Here's how data can be used in a POST:
payload = 'foobar'
r = requests.post('http://httpbin.org/post', data=payload)
print(r.text)
Which outputs
{
"args": {},
"data": "foobar",
[...]
"url": "http://httpbin.org/post"
}
Notice how the POST data does not show up in the query strings, as they are transmitted through the body of the request instead.
Critique of this answer has pointed out that there are more options. I never denied such a thing in my original answer, but let's take a closer look.
The documentation examples always show:
But that doesn't mean they are mutually exclusive.
In theory you could mix the two together in a POST:
data = 'foobar'
params = {'key1': 'value1', 'key2': 'value2'}
r = requests.post('http://httpbin.org/post', params=params, data=data)
print(r.text)
Which outputs
{
"args": {
"key1": "value1",
"key2": "value2"
},
"data": "foobar",
[...]
"url": "http://httpbin.org/post?key1=value1&key2=value2"
}
But you cannot mix data into a GET:
data = 'foobar'
params = {'key1': 'value1', 'key2': 'value2'}
r = requests.get('http://httpbin.org/get', params=params, data=data)
print(r.text)
Outputs:
{
"args": {
"key1": "value1",
"key2": "value2"
},
[...]
"url": "http://httpbin.org/get?key1=value1&key2=value2"
}
Notice how the data field is gone.
First of all, there are two different methods:
requests.post()
makes a POST request (placing all the parameters in the body)requests.get()
makes a GET request (placing all the parameters in the URL)Then, according to the docs, you can choose between two parameters to send all the key/value data:
params=
, without string modifications.data=
, applying a form-encoding string modification to the parameters.So, you have 4 choices to send your request:
requests.post(url, params=)
requests.post(url, data=)
requests.get(url, params=)
requests.get(url, data=)
I don't think the currently accepted answer is correct. He is actually talking about requests.post()
but using requests.get()
in his own example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With