Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

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!

question from:https://stackoverflow.com/questions/24535920/difference-between-data-and-params-in-python-requests

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...