在Python 3.6中从HTTPResponse到str
从对VimeoAPI的POST请求中,我得到一个编码为HTTPResponse的JSON对象在Python 3.6中从HTTPResponse到str,python,json,api,vimeo,Python,Json,Api,Vimeo,从对VimeoAPI的POST请求中,我得到一个编码为HTTPResponse的JSON对象 r = http.request('POST', 'https://api.vimeo.com/oauth/authorize/client?grant_type=client_credentials', headers={'Authorization': 'basic XXX'}) 我没有找到将HTTPResponse转换为str或Json对象的方法。在stackoverflow中,我找到并尝试了以
r = http.request('POST', 'https://api.vimeo.com/oauth/authorize/client?grant_type=client_credentials', headers={'Authorization': 'basic XXX'})
我没有找到将HTTPResponse转换为str或Json对象的方法。在stackoverflow中,我找到并尝试了以下选项:
json.loads(r.decode('utf-8'))
json.loads(r.readall().decode('utf-8'))
str(r, 'utf-8')
但没有一个成功
请问你能帮忙吗
谢谢尝试使用请求模块
import requests
import json
r=requests.post('https://api.vimeo.com/oauth/authorize/client?grant_type=client_credentials', varData, headers={'Authorization': 'basic XXX'})
response = json.loads(r.text)
尝试使用请求模块
import requests
import json
r=requests.post('https://api.vimeo.com/oauth/authorize/client?grant_type=client_credentials', varData, headers={'Authorization': 'basic XXX'})
response = json.loads(r.text)
来自(我的):
class http.client.HTTPResponse(sock,debuglevel=0,method=None,url=None)
类,其实例在成功连接时返回不由用户直接实例化。
而且:
另请参见建议将Requests包用于更高级别的HTTP客户端接口
所以你最好直接使用
发出请求后,只需使用(我的)中的json.loads(r.text)
:
class http.client.HTTPResponse(sock,debuglevel=0,method=None,url=None)
类,其实例在成功连接时返回不由用户直接实例化。
而且:
另请参见建议将Requests包用于更高级别的HTTP客户端接口
所以你最好直接使用
发出请求后,只需使用
json.loads(r.text)
use可以使用http.client模块。例如:
import http.client
import json
conn = http.client.HTTPConnection('https://api.vimeo.com/oauth/authorize/client?grant_type=client_credentials')
headers = {'Authorization': 'basic XXX'}
params = varData
conn.request('POST', '', params, headers)
response = conn.getresponse()
content = bytes.decode(response.read(), 'utf-8') #return string value
res_map = json.loads(content) #if content is json string
有关更多信息,请参阅:使用可以使用http.client模块。例如:
import http.client
import json
conn = http.client.HTTPConnection('https://api.vimeo.com/oauth/authorize/client?grant_type=client_credentials')
headers = {'Authorization': 'basic XXX'}
params = varData
conn.request('POST', '', params, headers)
response = conn.getresponse()
content = bytes.decode(response.read(), 'utf-8') #return string value
res_map = json.loads(content) #if content is json string
有关更多信息,请参阅:是否没有
r.text
或r.body
属性?dir(r)
Hi@RSHAP no r.text或r.body属性的输出是什么。dir(r)的输出是['CONTENT_DECODERS'、'REDIRECT_Status'、'abstractmethods'、'class'、'del'、'delattr'、'dir'、'doc'、'enter'、'eq'、'exit'、'format'、'ge'、'getattribute'、'gt'、'hash'、'init'、'init_subclass'、'iter'、'le'、'lt'、'module'、'ne'、'new'、'next'、'reduce'、'reduce_-lasshook',[…]是否没有r.text
或r.body
属性?dir(r)Hi@RSHAP no r.text或r.body属性的输出是什么。dir(r)的输出是['CONTENT_DECODERS'、'REDIRECT_Status'、'abstractmethods'、'class'、'del'、'delattr'、'dir'、'doc'、'enter'、'eq'、'exit'、'format'、'ge'、'getattribute'、'gt'、'hash'、'init'、'init_subclass'、'iter'、'le'、'lt'、'module'、'ne'、'new'、'next'、'reduce'、'reduce_-套索',[…]