lukehaas / RunJS

RunJS is a JavaScript playground for macOS, Windows and Linux. Write code with instant feedback and access to Node.js and browser APIs.
https://runjs.app
2.01k stars 43 forks source link

HTTP requests always show status 200 (even when they're not) #563

Closed acnebs closed 6 months ago

acnebs commented 1 year ago

Hi, I'm seeing some very strange behavior whenever I try to make an API call from RunJS. Namely, all requests show a 200 status code, even if the server is sending something else. Here is some example code:

const randomApiUrl = `https://api.openweathermap.org/data/3.0/onecall?lat=33.44&lon=-94.04&exclude=hourly,daily&appid=INVALID_API_KEY`;

try {
  const res = await fetch(randomApiUrl)
  // this gets executed even though there is an error
  console.log('ok', res.status)
  console.log(await res.json())
} catch (err) {
  // the server returns a 401, this should be executed instead
  console.log('err', err)
}

As you can see, I haven't actually specified an API key. The HTTP response code for this when I run it in Postman or my browser is 401, as you'd expect. However when I run this code in RunJS, the response code is 200 (and the catch is obviously never run). Is this known behavior? Struggling to write some network code with this issue. I've tried this both with the built-in fetch but also with the axios library and the problem is present regardless.

lukehaas commented 1 year ago

This is likely being caused by RunJS's CORS defeating solution. I'll take a look into this soon.

acnebs commented 1 year ago

Any update on this? I'd still like to use RunJS for testing web requests but this is a blocker for me.

lukehaas commented 1 year ago

I have a fix ready for the next release. This should be coming in a couple of months.

lukehaas commented 6 months ago

@acnebs this is now resolved in the latest version of RunJS.

acnebs commented 6 months ago

Can confirm that it is working for me, thanks for getting that fixed!