Python 请求模块和 JSON 响应

标签 python json http-request python-requests

我正在使用真棒 Requests module测试我为我们的一个内部项目创建的 API。 我相信我已经发现了 Requests 模块本身的缺陷,或者是我在使用它时的缺陷。

因为我们的数据不是 super 敏感,所以我们的 API 使用简单、基本的 HTTP 身份验证来控制访问。当我使用 JSON 作为数据格式并使用带有 HTTPBasicAuthHandler 的 urllib2 或 PHP 和 cURL 发出 API URL 请求时,我将数据作为格式正确的 JSON 字符串返回 - 没问题。

但是,当我使用 Requests 模块发出相同的请求时,我得到一个编码字符串,但我无法确定它是什么类型的编码。这是该字符串开头的片段:

\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\xadZ\xfb\x8f\xd3H\x12\xfeWzG\xab;\x90

以下是我使用 Requests 重现此问题的几行代码:

import requests
# api_user and api_pw not printed here for security reasons
r = requests.get('http://ourdomain.com/api/featured/school/json', auth=(api_user, api_pw))
status = r.status_code # Produces 200 every time
rawdata = r.read()
print rawdata

我每次这样做都会得到编码的字符串。

谁能帮我确定: a)那是什么编码(为了我自己的启发),以及 b) 为什么请求以该编码返回数据,以及如何解码和/或“修复”它。

提前致谢!

最佳答案

出于好奇,当您print r.content 时您会得到什么?

关于Python 请求模块和 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6998943/

相关文章:

python - 如何在 python 中自动化配置 AWS 命令​​行界面和设置配置文件过程?

java - 限制 REST 中的 json 参数

asp.net-mvc - 如何从 ASP.NET MVC Web API 帖子中获取原始 JSON 字典?

java - 使用http组件解析http请求服务器端

拉拉维尔 5.2 : How to access Request & Session Classes from own Event Listener?

python - 在 Django 中向我的模型添加历史 View

python - 为什么我得到这个python语法indexerror

html - 错误 : Element type is invalid: expected a string (for built-in components) or a class/function

java - 带 post 数据的 android HTTP 请求

python - 元组列表的列表,按第一个元素分组并添加第二个元素