当curl有效时,python请求模块不起作用.我究竟做错了什么? [英] python requests module doesnt work when curl works. What am I doing wrong?

查看:55
本文介绍了当curl有效时,python请求模块不起作用.我究竟做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个curl请求和一个使用python请求模块到本地Web服务的类似请求.当curl请求正常运行时,通过python发出的请求未按预期运行,即未返回json响应.

为什么会这样?使用python时,我仍然收到200响应,但是像curl中那样获得HTML响应文本而不是json,并且该响应与无效会话等有关.

这是卷曲请求

  root @ weve1:〜$ curl -k --GET --data"ajax = getPermissions& project = test& session.id = 5604d7ce-f3dd-4349-8957-563c2675ae5c" http://localhost:12320/经理{权限":[{权限":["ADMIN"],用户名":"azkaban"}],"project":"test","projectId":92} 

这与python中的请求相同

  root @ weve1:〜$ pythonPython 2.7.6(默认,2015年6月22日,17:58:13)linux2上的[GCC 4.8.2]键入帮助",版权",信用"或许可证"以获取更多信息.>>>汇入要求>>>s = requests.get('http://localhost:12320/manager',data = {'ajax':'getPermissions','project':'test','session.id':'5604d7ce-f3dd-4349-8957-563c2675ae5c'})>>>s.json()追溯(最近一次通话):在< module>中的文件< stdin>",第1行,JSON中的文件"/usr/lib/python2.7/dist-packages/requests/models.py",行741返回json.loads(self.text,** kwargs)载入中的文件"/usr/lib/python2.7/json/__init__.py",第338行返回_default_decoder.decode(s)文件"/usr/lib/python2.7/json/decoder.py",行366,在解码中obj,end = self.raw_decode(s,idx = _w(s,0).end())raw_decode中的文件"/usr/lib/python2.7/json/decoder.py",第384行引发ValueError(无法解码JSON对象")ValueError:无法解码JSON对象>>> 

解决方案

在curl手册页中:

-G/-获取

使用此选项时,将使用-d/-data或--data-binary指定的所有数据用于HTTP GET请求中,而不要使用原本应使用的POST请求.数据将以'?'附加到URL.分隔符.

因此curl实际上将参数作为查询字符串传递,即 http://localhost:12320/manager?ajax = getPermissions& project = test& session.id = 5604d7ce-f3dd-4349-8957-563c2675ae5c .

为了通过请求传递URL中的数据,您需要传递数据

This is the same request made in python

root@weve1:~$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> s=requests.get('http://localhost:12320/manager',data={'ajax':'getPermissions','project':'test','session.id':'5604d7ce-f3dd-4349-8957-563c2675ae5c'})
>>> s.json()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/requests/models.py", line 741, in json
    return json.loads(self.text, **kwargs)
  File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
>>> 

From the curl man page:

-G/--get

When used, this option will make all data specified with -d/--data or --data-binary to be used in a HTTP GET request instead of the POST request that otherwise would be used. The data will be appended to the URL with a '?' separator.

So curl is actually passing the parameters as a query string instead, i.e. http://localhost:12320/manager?ajax=getPermissions&project=test&session.id=5604d7ce-f3dd-4349-8957-563c2675ae5c.

In order to pass data in the URL with requests, you need to pass the data in the params argument:

data = { 'ajax': 'getPermissions', 'project': 'test', 'session.id': '5604d7ce-f3dd-4349-8957-563c2675ae5c' }
s = requests.get('http://localhost:12320/manager', params=data)

The data argument only refers to the actual request body.

这篇关于当curl有效时,python请求模块不起作用.我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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