0

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.

David K
  • 1
  • 1
  • What is `ic()`? – MattDMo May 24 '23 at 19:24
  • Icecream its a module that works like print() but is much more prettier – David K May 24 '23 at 19:26
  • What is the result of printing `result.text`? What was the result you were expecting to get? If possible, please produce a full [mre]. – SimonUnderwood May 24 '23 at 19:30
  • 1
    Welcome to Stack Overflow. Please read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592). You already know from the error message where the error is being reported - in the part where you ask it for the JSON-formatted version of the response data - and what the problem is - that the data is wrong from the beginning. So the next step should be to *check what the raw data is*, in order to understand what happens when you make that web request. – Karl Knechtel May 24 '23 at 19:53
  • Actually the issue is within the cookies, as of now they are overwriting each other. Which is causing the issue. I have put them into a dictionary, but the header is no longer going to work ofcourse – David K May 24 '23 at 19:55
  • The solution is to build the header in the `for` loop where you make the request. Store both the URL and the cookie in a list of dicts - one dict for each server. You then create, send, and process the requests all at once, instead of having to do at least two iterations as you're doing now. – MattDMo May 25 '23 at 21:50

0 Answers0