Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Flask: KeyError 'data' when fetching data from Jikan API

oezilot

New Coder
Hi everyone,

I'm running into a KeyError: 'data' when fetching data from the Jikan API in my Flask app. This used to work fine, and another program using the same API (also written by ChatGPT) is still functioning correctly, so the API itself should be working as expected.

Here is the relevant part of the code:
Python:
def fetchData():
    # store params und page in a session
    page = session.get('page', 1)
    params = session.get('params', {})
    # fetching the data with the api depending on the params and page
    response = requests.get(urlBuilder(page, params))
    # safe data in a variable
    if response.status_code == 200:
        data = response.json()
        return data['data']
    else:
        return None

My Questions:
  • Has anyone experienced the 'data' key missing from the Jikan API response, despite getting a 200 status code?
  • Could this issue be related to Flask sessions? I’m using session.get() to store and retrieve the page and params, and I’m wondering if the session data could be getting lost or not persisting correctly between requests.
  • Any ideas why this would suddenly stop working, even though the same API works fine in another program?
Thanks in advance for your help!
 
Hi everyone,

I'm running into a KeyError: 'data' when fetching data from the Jikan API in my Flask app. This used to work fine, and another program using the same API (also written by ChatGPT) is still functioning correctly, so the API itself should be working as expected.

Here is the relevant part of the code:
Python:
def fetchData():
    # store params und page in a session
    page = session.get('page', 1)
    params = session.get('params', {})
    # fetching the data with the api depending on the params and page
    response = requests.get(urlBuilder(page, params))
    # safe data in a variable
    if response.status_code == 200:
        data = response.json()
        return data['data']
    else:
        return None

My Questions:
  • Has anyone experienced the 'data' key missing from the Jikan API response, despite getting a 200 status code?
  • Could this issue be related to Flask sessions? I’m using session.get() to store and retrieve the page and params, and I’m wondering if the session data could be getting lost or not persisting correctly between requests.
  • Any ideas why this would suddenly stop working, even though the same API works fine in another program?
Thanks in advance for your help!
Hi there,

Might be a throttling issue...please check if your application is exceeding the API rate limit, and adjust accordingly. Let's rule that out of the way first
 
Hi Antero360,

Thank you for your suggestion about checking the API rate limits. After monitoring the response headers, I didn't encounter any 429 Too Many Requests errors, which leads me to believe that it wasn't a throttling issue. Instead, I was seeing an 500 Internal Server Error.

Interestingly, the issue seems to have resolved itself, and everything is working now, but I'm still unsure what exactly fixed it. I want to make sure it doesn't happen again in the future, so I’m trying to figure out what could have caused the error in the first place.

I really appreciate your help and would love any additional insights you might have on how to prevent this kind of internal server error going forward!
 
The following is my response header:

Response Headers: {'Server': 'nginx/1.24.0', 'Date': 'Tue, 22 Oct 2024 08:13:24 GMT', 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Cache-Control': 'must-revalidate, private', 'pragma': 'no-cache', 'expires': '-1', 'access-control-allow-origin': '*', 'Content-Encoding': 'gzip', 'Vary': 'Accept-Encoding', 'X-Cache-Status': 'HIT', 'X-Powered-By': 'the-power-of-friendship'}

Nothing seems too suspicious, but could there be an issue with the X-Cache-Status: HIT? I’m wondering if cached responses might be causing problems, even though the Cache-Control headers indicate that the data should be revalidated.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom