Making HTTP Requests and processing JSON Response in python

We can make HTTP GET requests with python libraries like urllib , httplib and request.
In the below example we will make an API call with requests library and process
the JSON data returned from server.

import requests
import json


base_url = ''
full_url= ''

PARAMS = {'symbol':'BTCUSDT'}

#Request with parameters
response = requests.get( url = base_url , params = PARAMS , verify = False )
data = response.json()

#Request with full API request
resp = requests.get(full_url,verify=False)
temp_data = json.dumps(resp.json())
data_json = json.loads(temp_data)

print( data_json["symbol"] + " : " + data_json["price"] )

Here we are fetching the current cryptocurrency price from binnace API calls. First request is made with the full API url . The second call is made y providing the GET parameters to the api end point. Both the calls return same data. The response contains both response_code ( response.status_code ) and response_data ( response.text )

Output of the program.

<Response [200]>
{'symbol': 'BTCUSDT', 'price': '56127.48000000'}
BTCUSDT : 56149.99000000



Leave a Reply