Python请求模块发送JSON字符串而不是x-www-form-urlencoded参数字符串(Python requests module sends JSON string instead of x-www-form-urlencoded param string)
我的印象是使用x-www-form-urlencoded规范的POSTS应该在帖子的正文中发送一个URL编码的参数字符串。 但是,当我这样做
data = json.dumps({'param1': 'value1', 'param2': 'value2'})
Requests.post(url, data=data)
接收端请求的主体如下所示:
{"param1": "value1", "param2": "value2"}
但我期待得到这个
param1=value1¶m2=value2
我如何获得以第二种形式发送数据的请求?
I was under the impression that POSTSs using x-www-form-urlencoded specifications should send a URL encoded param string in the body of the post. However, when I do this
data = json.dumps({'param1': 'value1', 'param2': 'value2'})
Requests.post(url, data=data)
The body of the request on the receiving end looks like this:
{"param1": "value1", "param2": "value2"}
But I was expecting to get this
param1=value1¶m2=value2
How I can get Requests to send the data in the second form?
原文:https://stackoverflow.com/questions/26615756
更新时间:2019-11-27 06:56
最满意答案
你得到JSON的原因是你明确调用了json.dumps来生成一个JSON字符串。 只是不这样做,你不会得到一个JSON字符串。 换句话说,将第一行更改为:
data = {'param1': 'value1', 'param2': 'value2'}
正如文档解释的那样,如果你传递一个字典作为data值,它将被编码,而如果你传递一个字符串,它将被发送。
例如,在一个终端窗口中:
$ nc -kl 8765
在另一个:
$ python3
>>> import requests
>>> d = {'spam': 20, 'eggs': 3}
>>> requests.post("http://localhost:8765", data=payload)
^C
>>> import json
>>> j = json.dumps(payload)
>>> requests.post("http://localhost:8765", data=j)
^C
在第一个终端中,你会看到第一个请求体是这个(和Content-Type application/x-www-form-urlencoded ):
spam=20&eggs=3
...而第二个是这个(并没有内容类型):
{"spam": 20, "eggs": 3}
The reason you're getting JSON is because you're explicitly calling json.dumps to generate a JSON string. Just don't do that, and you won't get a JSON string. In other words, change your first line to this:
data = {'param1': 'value1', 'param2': 'value2'}
As the docs explain, if you pass a dict as the data value, it will be form-encoded, while if you pass a string, it will be sent as-is.
For example, in one terminal window:
$ nc -kl 8765
In another:
$ python3
>>> import requests
>>> d = {'spam': 20, 'eggs': 3}
>>> requests.post("http://localhost:8765", data=payload)
^C
>>> import json
>>> j = json.dumps(payload)
>>> requests.post("http://localhost:8765", data=j)
^C
In the first terminal, you'll see that the first request body is this (and Content-Type application/x-www-form-urlencoded):
spam=20&eggs=3
… while the second is this (and has no Content-Type):
{"spam": 20, "eggs": 3}
2014-10-28
相关问答
你得到JSON的原因是你明确调用了json.dumps来生成一个JSON字符串。 只是不这样做,你不会得到一个JSON字符串。 换句话说,将第一行更改为: data = {'param1': 'value1', 'param2': 'value2'}
正如文档解释的那样,如果你传递一个字典作为data值,它将被编码,而如果你传递一个字符串,它将被发送。 例如,在一个终端窗口中: $ nc -kl 8765
在另一个: $ python3
>>> import requests
>>> d =
...
您将User.archive(w: thing)作为嵌入请求正文中的data传递,这可能永远不会起作用。 通常,您的archive(w:)和unarchive(d:)永远不会生成任何有用的结果,您最好立即删除它们。 如果要传递需要x-www-form-urlencoded参数,则需要创建类似URL查询的字符串。 尝试这样的事情: func login(username: String, password: String, completionHandler: @escaping (Login) -
...
解决方案是传递标题“content-type:application / x-www-form-urlencoded; charset-utf-8”,我尝试使用PostMan,但我发现,基本上,PostMan忽略了标题内容类型当使用post方法并选择已经“x-www-form-urlencoded”单选按钮选项时。 使用cUrl -H "content-type: application/x-www-form-urlencoded; charset=UTF-8" 做了这个工作。 The solut
...
如果你想用排球你可以检查我的答案 编译'com.android.volley:volley:1.0.0' private void getVolley(final String token, String url) {
final RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest jsonObjRequest = new StringRequest
...
InputJSON应该是这里的POST参数名称,所以它不应该与之间的数据连接在一起。 它应该遵循正常的name=value格式。 InputJSON is supposed to be the POST parameter name here, so it should not be concatenated with the data with : in between. It should follow the normal name=value format.
是的是Jsoup,有一个拉动请求来解决这个问题,希望将被包含在下一个版本中(1.9.2之后) https://github.com/jhy/jsoup/pull/734 Yes is Jsoup, there is a pull request to fix this issue, hopefully will be included in the next release (after 1.9.2) https://github.com/jhy/jsoup/pull/734
此错误与bean验证无关。 首先, application/x-www-form-urlencoded不适用于像MyBean这样的任意类型的MessageBodyReader 。 只读的类型可以处理Form和MultivaluedMap 。 所以这些是你可以作为身体参数的唯一两种类型。 但还有另一种选择。 @BeanParam注解允许你创建一个bean来结合任意的@XxxParam注释元素,比如@FormParam , @PathParam , @QueryParam , @HeaderParam
...
使用request.forms.get('spam') http://XX.XX.XX.XXX:8080/ocr_response2网址也应该是http://XX.XX.XX.XXX:8080/ocr_response2 use request.forms.get('spam') also in requests.post url should be http://XX.XX.XX.XXX:8080/ocr_response2
我设法让客户端的东西工作。 问题是我强制将文件作为单独的消息正文部分发送,而x-www-form-urlencoded实际上将所有数据作为参数打包在整个正文的查询中 。 因此,如果您想通过Jersey post方法发送附件,那么正常工作的客户端代码将是: ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource webResource = client.re
...
通常,POST请求不需要查询字符串,但它仍然受到服务器逻辑实现的影响。 如果OAuth是众所周知的标准并且它们遵循良好实践,则使用表单编码数据是安全的,除非在API中明确提到将参数作为查询字符串发送。 Query String&Post数据是两组不同的参数。 如果服务器需要查询字符串,则必须仅发送查询字符串。 这完全取决于服务器端逻辑的实现方式。 你不能交替使用它们。 大多数API文档明确指出了他们期望的内容。 In general, POST requests do not need query
...