Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "data" and "params" in Python requests?

I was curious what the difference was between the data parameter and the params parameter in a python-requests request, and when each should be used.

One example is I have an array of dicts users=[{"email_hash": "fh7834uifre8houi3f"}, ... ] and I try to do a POST (requests.post()) with

params = {     "ads_token": blah blah,     "user_id": blah blah,     "users": json.dumps(users)  # users=[{"email_hash": "fh7834uifre8houi3f"}, ... ]     "hash_type": "md5" } 

and because users is a few hundred long, the resulting string from json.dumps(users) (and thus the URL itself as well) is very long and I get the error {'status_code': 414, 'reason': 'Request-URI Too Large'}. Would this be a case for data or is there some other path I should follow? Thanks!

like image 200
tscizzle Avatar asked Jul 02 '14 16:07

tscizzle


People also ask

What are params in requests?

params is for GET-style URL parameters, data is for POST-style body information. It is perfectly legal to provide both types of information in a request, and your request does so too, but you encoded the URL parameters into the URL already. Your raw post contains JSON data though.

What is the difference between query string params and form data params?

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.

What is params data?

Pass user-supplied data to calculated fields and connectors. Parameters let you interact with user-provided data. For example, you can create calculated fields that include input from people using your report, or pass values back to the SQL query used by your data source.

How do you pass parameters in POST request URL in Python?

To send parameters in URL, write all parameter key:value pairs to a dictionary and send them as params argument to any of the GET, POST, PUT, HEAD, DELETE or OPTIONS request. then https://somewebsite.com/?param1=value1&param2=value2 would be our final url.


1 Answers

params form the query string in the URL, data is used to fill the body of a request (together with files). GET and HEAD requests have no body.

For the majority of servers accepting a POST request, the data is expected to be passed in as the request body.

You need to consult the documentation for the specific API you are calling as to what they expect, but if you have to assume, assume you have to use data.

like image 133
Martijn Pieters Avatar answered Sep 22 '22 12:09

Martijn Pieters