简体   繁体   中英

I dont get why I cant get a json format with requests.Request()?

from requests import Request as R

markets = R("GET", "https://ftx.com/api/markets")
print(markets.json())

Error: print(markets.json()) TypeError: 'NoneType' object is not callable

Process finished with exit code 1

I want to get the HTTP response as json but it doesnt work although it works with requests.get(). Help please?

Request is just the object that represents the request. You want requests.request to construct and make the request.

import requests

markets = requests.request("GET", "https://ftx.com/api/markets")

The json attribute of a Request object would be the JSON payload of the request, not the JSON response.

To make a request manually with a Request object, you need to first prepare it, then use a session to send the request. For example,

markets = requests.Request("GET", "https://ftx.com/api/markets")
r = markets.prepare()
s = requests.Session()
result = s.send(r)
print(result.json())
example:

# import requests module
import requests
 
# Making a get request
response = requests.get('https://api.github.com')
 
# print response
print(response)
 
# print json content
print(response.json())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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