“数据"和“数据"之间的区别和“参数"在 Python 请求中? [英] Difference between "data" and "params" in Python requests?

查看:18
本文介绍了“数据"和“数据"之间的区别和“参数"在 Python 请求中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇 python-requests 请求中的 data 参数和 params 参数之间有什么区别,以及何时应该使用.

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.

一个例子是我有一个字典数组 users=[{"email_hash": "fh7834uifre8houi3f"}, ... ] 并且我尝试做一个 POST (requests.post()) 与

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"
}

并且因为 users 有几百长,所以 json.dumps(users) 的结果字符串(因此 URL 本身也是如此)很长,我得到错误 {'status_code': 414, 'reason': 'Request-URI Too Large'}.这是 data 的情况还是我应该遵循的其他路径?谢谢!

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!

推荐答案

params 形成 URL中的查询字符串data用于填充请求的body(连同files).GETHEAD 请求没有正文.

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.

对于接受 POST 请求的大多数服务器,数据应作为请求正文传入.

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

您需要查阅您正在调用的特定 API 的文档以了解他们的期望,但如果您必须假设,则假设您必须使用 data.

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.

这篇关于“数据"和“数据"之间的区别和“参数"在 Python 请求中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆