简体   繁体   English

Python REST API问题

[英]Python REST API Issues

I am working on a tool that queries a number of APIs, one of which is a RESTful API. 我正在研究一种查询许多API的工具,其中一个是RESTful API。 All of the other functions (API calls) of my program work fine with requests.get(), however with the REST API, I do not seem to be able to access the actual content of the response, only the status code. 我程序的所有其他功能(API调用)都可以与request.get()一起正常工作,但是使用REST API,我似乎无法访问响应的实际内容,只能访问状态代码。 ie when I simply print the response, (not response.status_code) I get: <Response [200]> output to the screen. 即,当我仅打印响应(而不是response.status_code)时,我得到: <Response [200]>输出到屏幕。 Any ideas? 有任何想法吗?

Snippet of code: 代码段:

# The URL is correct in my program, For sure.
url = ('http://APIurl/%s' % entry)
    try:
       response = requests.get(url)

       # prints <Response [200]>
       print response

       # Fails, expecting JSON that isn't there
        results.append(response.json())

打印响应对象的属性以查看其可用属性:

print response.__dict__

如果内容无效json,则response.text是您的朋友。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM