Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble connecting to localhost from browser using WSL

I am trying to develop a Python FastAPI endpoint on Windows in a Ubuntu WSL environment but seem to be unable to view/access this from my browser. The weird thing is that I tried this a few days ago and it seemed to work fine but hasn't worked since as I seem to get this error from using curl in my powershell:

curl : Unable to connect to the remote server
At line:1 char:1
+ curl http://127.0.0.1:8000/
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

And in any browser I get an error where it can't find that server.

Here's some example code:

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {
        "message": "Hello World"
    }

and then I run uvicorn main:app --reload to start the app and try to access the endpoint provided.

My OS build version is 19041.508 if that matters.

Does anyone know how I can get this working again? I have no idea what could be going wrong here.

Thanks

like image 633
sohaib Avatar asked Dec 09 '25 06:12

sohaib


1 Answers

Restarting the computer can sometimes fix odd connection issues. I'm not sure why. I was having trouble with a fastapi server refusing a secure connection, which previously worked and worked with curl but not in the browser, despite all sorts of measures. It mysteriously would refuse any connection from a browser. Restarted the computer and updated VS Code and everything seems to be working fine now. Maybe it was the VS Code update.

like image 177
Bryan Aneux Avatar answered Dec 11 '25 19:12

Bryan Aneux



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!