When I run the below code I get a JSONDecode error, but it comment out Server2 it works fine. I'm getting valid responses from both API calls if run separately. My problem is when I hit the line data = response.json() anyway to resolve this error?
import requests
import json
import pandas as pd
from pandas import json_normalize
# Quick debugging tool
from icecream import ic
# ic.configureOutput(prefix="debug -> ")
ic.configureOutput(includeContext=True)
ic.enable()
cookie_urls = [
'http://server1.com/omk/opCharts/login?',
'http://server2.com/omk/opCharts/login?',
for urls in cookie_urls:
authresponse = requests.post(urls)
authdict = authresponse.headers
ic(authdict)
mycookie = authdict["Set-Cookie"]
ic(mycookie)
payload = []
headers = {"Content-Type": "application/x-www-form-urlencoded", "Cookie": mycookie}
# URL's for API calls using the cookie obtained above
app_url = [
'http://server1.com',
'http://server2.com',
]
api_endpoints = '/en/omk/opCharts/v1/nodes?query=["config.nodeType","switch"]&properties=["info."]',
url_devices = []
for server in app_url:
for api in api_endpoints:
url = server + api
url_devices.append(url)
data_list = [] # List to store the JSON data from each response
# Make API requests
for url in url_devices:
response = requests.get(url, headers=headers, data=payload)
ic(url)
ic(response)
if response.status_code == 200:
data = response.json()
data_list.append(data)
else:
print(f"Error retrieving data from {url}: {response.status_code}")
Commenting out seperate API's the code will perform as expected but if i try to make multiple calls it fails.