Python Requests Get(URL)200响应,无数据。

3

我正在使用request库进行GET请求。我可以获得200响应,但它没有返回任何数据,仅返回对象“jobs”,但之后没有其他内容。我正在使用Visualping.io api。我已经成功运行了CURl命令,并从浏览器URL中获得成功。这是我的Python代码。我已编辑掉了我的凭据和PHP sesh id。

`import requests
r = requests.get("https://visualping.io/api/job/list", headers={'username':'myemail@email.com', 'password':'MyPassword', 'User-Agent':'test'})
print (r.content)
print (r.status_code)
print (r.headers)
print (r.json)`

我还尝试过不将用户和密码作为标头传递,而是像这样将它们直接传递到URL中。同样,在浏览器和curl中都能正常工作。

`https://visualping.io/api/job/list?username='myusername'&password='mypassword'`

对于这两个,我得到以下输出

//printcontent {"success":true,"jobs":[]}

//打印状态码 200

//下面是打印标头

{'X-Powered-By': 'PHP/5.5.35', 'Transfer-Encoding': 'chunked', 'Set-Cookie': 'PHPSESSID={MYSESSIONID}; expires=Fri, 26-May-2017 20:42:31 GMT; Max-Age=3600; path=/', 'Expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'Vary': 'Accept-Encoding', 'Server': 'nginx', 'Connection': 'keep-alive', 'Pragma': 'no-cache', 'Cache-Control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 'Date': 'Fri, 26 May 2017 19:42:31 GMT', 'Content-Type': 'text/html', 'Content-Encoding': 'gzip'}

//这是打印json {u'jobs': [], u'success': True}

这是整块内容

`{"success":true,"jobs":[]}
200
{'X-Powered-By': 'PHP/5.5.35', 'Transfer-Encoding': 'chunked', 'Set-Cookie': 'PHPSESSID={MYSESSIONID}; expires=Fri, 26-May-2017 20:43:47 GMT; Max-Age=3600; path=/', 'Expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'Vary': 'Accept-Encoding', 'Server': 'nginx', 'Connection': 'keep-alive', 'Pragma': 'no-cache', 'Cache-Control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 'Date': 'Fri, 26 May 2017 19:43:47 GMT', 'Content-Type': 'text/html', 'Content-Encoding': 'gzip'}
<bound method Response.json of <Response [200]>>`

https://company-32327.frontify.com/d/Lr7wNKb1omxI/visualping-api

以下是文档中预期响应:

GET /api/job/list

`{
    "jobs": {
        "active": [
          {
            "id": "NzqkVe1AI6WYBli",
            "created": "2015-09-06 00:37:16",
            "url": "www.google.de",
            "description": "Google Landing Page",
            "runs": "10",
            "trigger": "1",
            "interval": "60",
          }  
        ],
        "inactive": [
          {
            "id": "gCXHiydaCulFOFA",
            "created": "2016-09-06 00:37:16",
            "url": "www.bing.de",
            "description": "Bing Landing Page",
            "runs": "25",
            "trigger": "10",
            "interval": "300"
          }  
        ],
    }
}`

更新:不传递凭证将产生相同的结果“success”:true,“jobs”:[]} 200 {'X-Powered-By':'PHP / 5.5.35','Transfer-Encoding':'chunked','Set-Cookie':'PHPSESSID = 9aq0m56d1otm617hoidnl40np4; expires = Fri,26-May-2017 21:12:55 GMT; Max-Age = 3600; path = /','Expires':'Thu,19 Nov 1981 08:52:00 GMT','Vary':'Accept-Encoding','Server':'nginx','Connection':'keep-alive','Pragma':'no-cache','Cache-Control':'no-store,no-cache,must-revalidate,post-check = 0,pre-check = 0','Date':'Fri,26 May 2017 20:12:55 GMT','Content-Type':'text / html','Content-Encoding':'gzip'} - Goldfish
2个回答

5

json 是一个函数,请尝试使用以下代码:

print(r.json())

所以,你只缺少括号。从你的输出可以看出,你正在访问一个方法:
<bound method Response.json of <Response [200]>>

4
您链接的VisualPings API文档显示,其仅支持HTTP基本身份验证。因此,请尝试以下操作:
import requests
from requests.auth import HTTPBasicAuth
r = requests.get("https://visualping.io/api/job/list", auth=HTTPBasicAuth('myusername', 'mypassword'))
print(r.json())

Requests基础认证文档

,该文档涉及IT技术相关内容。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接