JSON formatting


Hey everyone, I apologize if this may seem rather elementary but I’m very new to APIs in general and I’ve been having some difficulties obtaining the proper json file. I’m looking at the documentation and adding .json at the end of the URL but for some reason my request is failing. However, if I remove it, then the request is working but I am not able to print the json file, which I’m presuming is because it is as an HTML. Any help would be much appreciated. Thank you. This is the code that I’m using right now:


This works fine. Set your Content-Type header to application/json

import requests
import json

url = "https://api.solcast.com.au/radiation/forecasts"

api_key = "YOUR_API_KEY"
params = {
  'api_key': api_key,
  'longitude': -73.539848,
  'latitude': 40.79204

headers = {
  'Content-Type': 'application/json'

response = requests.request("GET", url, headers=headers, params=params)

if response:
  print('An error occurred')

json_response = response.json()
text_response = response.text

parsed = json.loads(text_response)

if json_response == parsed:
  print(json.dumps(parsed, indent=4))

Thank you so much for the feedback. I really appreciate it.

1 Like