2017-05-26 235 views
0

我正在使用请求库来执行GET请求。我可以得到一个200响应,但它不返回任何数据,它返回对象的'作业',但没有别的。我正在使用Visualping.io api。我已经成功地运行CURL命令,并从浏览器URL成功运行....这是我的Python代码。我编辑了我的凭据和PHP sesh id。Python请求获取(URL)200响应,无数据

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

我自己也尝试没有用户,并通过页眉,和刚刚过去的他们在这样的网址..再次,这工作从浏览器和卷曲

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

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

// printcontent { “成功”:真正的 “工作”:[]}

//打印状态码200

//下面是打印页眉

{'X-Powered-By':'PHP/5.5.35','Transfer-Encoding':'chunked','Set-Cookie':'PHPSESSID = {MYSESSIONID }; expires = Fri,26-May-2017 20:42:31 GMT;最大年龄= 3600; '''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'}

// This is print 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 /工作/列表

`{ 
    "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" 
      } 
     ], 
    } 
}` 
+0

更新:通过没有凭据产生相同的结果“成功”:true,“jobs”:[]} {'X-Powered-By':'PHP/5.5.35','Transfer-Encoding':'chunked ','Set-Cookie':'PHPSESSID = 9aq0m56d1otm617hoidnl40np4; expires = Fri,26-May-2017 21:12:55 GMT;最大年龄= 3600; '''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

回答

1

链接到您的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 Basic Auth docs

2

json是一个函数预期的响应,请试试这个:

print(r.json()) 

所以,你只是缺少括号。你访问一个方法,你可以在输出中看到:

<bound method Response.json of <Response [200]>>