The problem seems extremely simple; I just want my FastAPI server to accept requests from my phone and my emulator. However, it does not.
- Note that I have no network problems. I have my node server on this same computer, and I can freely send requests to it and get a response.
I can send requests from my browser, and postman. No problem with that.
from enum import Enum
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
origins = [
"http://localhost.tiangolo.com",
"https://localhost.tiangolo.com",
"http://localhost",
"http://localhost:8080",
"http://myPhoneIp",
"https://myPhoneIp:8080",
"http://emulatorIp",
"https://emulatorIp",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=False,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def root():
print('someone sent a request')
return {"message": "Hello World"}
Why is this small piece of code not working? I am not using any cookies, etc. So I set allow_credentials=False
, and even if I set it to True
, it is still not working.
I even tried allow_origins=["*"], allow_credentials=False
still nothing.
Another thing to remember here is when I set allow_origins=[]
, it should not allow any request, even from localhost
, right? Why is it still allowed from the browser and Postman?
I know this issue looks similar to this How can I enable CORS in FastAPI? , but I have tried every solution in there, but nothing helped.
Here is flutter error when sending request E/flutter (23012): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Connection refused
My phone browser also says the connection was refused. I dug everywhere, but could not find a solution. Please help.