jikan-me / jikan-rest

The REST API for Jikan
https://docs.api.jikan.moe/
MIT License
413 stars 268 forks source link

🐛 Connection timeout in node and python #526

Closed meronogbai closed 2 months ago

meronogbai commented 3 months ago

Is there an existing issue for this?

Current Behavior

Connecting to api.jikan.moe keeps timing out in node and python runtimes.

Python example:

import requests

url = "https://api.jikan.moe/v4/anime/20/full"

response = requests.get(url)

if response.status_code == 200:
    data = response.json()

    print("Title:", data.get("data", {}).get("title", "No title found"))
else:
    print(f"Error retrieving data: HTTP Status Code {response.status_code}")

Node.js example

fetch('https://api.jikan.moe/v4/anime/20/full').then(console.log);

Weirdly enough, it works in postman, but not when I run this inside node or python. What could be the issue here?

Expected Behavior

I expected this api to work when I call it from node or python.

Steps To Reproduce

Copy the code snippets above and run them in your machine.

Environment

Used Public API
OS: macOS 14.4.1 23E224 arm64
Runtime: nodejs 18.18.2 and python 3.11.7

Anything else?

No response

irfan-dahir commented 3 months ago

Not sure why this is happening as I'm not too familiar with node or python but seems like a localized issue.

Can you check the logs or thrown exceptions to find out why the requests are timing out?

Also there are community made node and python wrappers available, can you see if those work for you: https://github.com/jikan-me/jikan-rest?tab=readme-ov-file#wrappers

meronogbai commented 3 months ago

@irfan-dahir I tried the jikan4 wrapper and it didn't work either.

Logs for node code snippet:

❯ node ./test.js
node:internal/deps/undici/undici:11372
    Error.captureStackTrace(err, this);
          ^

TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11372:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  cause: _ConnectTimeoutError: Connect Timeout Error
      at onConnectTimeout (node:internal/deps/undici/undici:6616:28)
      at node:internal/deps/undici/undici:6574:50
      at Immediate._onImmediate (node:internal/deps/undici/undici:6605:13)
      at process.processImmediate (node:internal/timers:476:21) {
    code: 'UND_ERR_CONNECT_TIMEOUT'
  }
}

Node.js v18.18.2

The python snippet just hangs indefinitely...

meronogbai commented 3 months ago

but seems like a localized issue

That's interesting. Can you tell me more?

irfan-dahir commented 3 months ago

Is your node/python on a separate environment (VPS, separate device, VM, WSL, etc)? If so, can you check if that environment has a working internet connection by trying out a curl command or perhaps pinging some other site or API?

irfan-dahir commented 3 months ago

Also, is this behavior consistent or just happening sometimes?

irfan-dahir commented 3 months ago

Is your node/python on a separate environment (VPS, separate device, VM, WSL, etc)? If so, can you check if that environment has a working internet connection by trying out a curl command or perhaps pinging some other site or API?

This could likely be due to firewall as well, so please look into that too

meronogbai commented 3 months ago

Nah it's all on my machine and it happens all the time.

https://github.com/jikan-me/jikan-rest/assets/22526062/a0237a77-4e47-48cf-b8f0-423fb7385941

It times out when I call the api inside node, and hangs when I call it via python and even wget. It doesn't time out when I run it inside the browser (using js), postman or curl.

meronogbai commented 3 months ago

contents of test.js

fetch('https://api.jikan.moe/v4/anime/20/full').then(console.log);
irfan-dahir commented 3 months ago

Unfortunately, due to my lack of familiarity with Python or Node, I'm unable to understand why this might be happening. Perhaps someone can chime in if they know what's wrong here.

Meanwhile, can you try out some other API so we can confirm whether this issue is directly related to Jikan or could be some code/environment-related factor?

meronogbai commented 3 months ago

No worries!

meronogbai commented 3 months ago

I tried using a different api and it doesn't have this issue.

fetch('https://cat-fact.herokuapp.com/facts')
  .then((response) => response.json())
  .then(console.log);
irfan-dahir commented 3 months ago

@meronogbai I was looking into the error thrown here:

code: 'UND_ERR_CONNECT_TIMEOUT'

Which led me to this: https://undici.nodejs.org/#/?id=network-address-family-autoselection

Someone posted a solution to this here: https://stackoverflow.com/a/76512104

Can you try and see if it works?

meronogbai commented 3 months ago

Yo that's an awesome clue

meronogbai commented 3 months ago

For posterity, I fixed this by running node --enable-network-family-autoselection ./test.js instead of node test.js.