a year ago
I'm having a problem making requests to my fastapi backend in python.
all my requests give this problem:
Access to fetch at 'https://back-email.felipe.fun/text' from origin 'https://emails.felipe.fun' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
62 Replies
a year ago
Did you configure CORS in your fastapi backend?
https://fastapi.tiangolo.com/tutorial/cors/#use-corsmiddleware
a year ago
In your case it'd be similar to:
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
origins = [
"https://emails.felipe.fun",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
)I did the 'cors' configuration and I still have the same problem
my original cors code
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
def setup_cors(app: FastAPI):
origins = [
"https://emails.felipe.fun"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)I removed my settings and left only the one you sent and I still have the same problem, I just need to make it work even if it is not safe
a year ago
Your config looks fine 🤔
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["gunicorn", "-k", "uvicorn.workers.UvicornWorker", "main:app", "--bind", "0.0.0.0:8000", "--workers", "4"]my dockerfile
PS C:\Users\felip> curl.exe -i -X OPTIONS https://back-email.felipe.fun/text
HTTP/1.1 502 Bad Gateway
Content-Length: 109
Content-Type: application/json
Server: railway-edge
X-Railway-Edge: railway/us-east4-eqdc4a
X-Railway-Fallback: true
X-Railway-Request-Id: PZaTRNFeRYaLYKqNwoOzXw
Date: Fri, 03 Oct 2025 00:03:37 GMT
{"status":"error","code":502,"message":"Application failed to respond","request_id":"PZaTRNFeRYaLYKqNwoOzXw"}
PS C:\Users\felip>
when I run a 'curl' I get this other problem.
a year ago
Let’s try a couple things:
- Did you put the domain to listen to port 8000? You could also put ENV var
PORTwith8000 - Unless you opted for the IPv4 feature flag, make your app listen to ipv6
--bind "[::]:8000" \I set it to port 8000 in the domain and created a variable PORT = 8000, now I will try to point it to IPV6
yes I have a separate front end, this is the link: https://github.com/FelipeRos19/FrontendDesafioAutoU
I did the test and I still have the same error.
I'm trying to modify the backend dockerfile
uvicorn app.main:app --host :: --port ${PORT:-8000} --workers ${WORKERS:-3} --log-level info --proxy-headers
I use this to run my fastapi projects
it was already in this mode, so I don't think the problem is with cloudflare, I will test the railway domain directly
I think you are getting an error deploying the backend, because the url is not working tho
I'm using https://www.nltk.org/ for some parts of the system, I'll add the files to the dockerfile to avoid downloading at startup

my first time using fastapi, this project is a technical challenge and definitely not my best performance haha
FROM python:3.12
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD uvicorn app.main:app --host 0.0.0.0 --port ${PORT:-8000} --log-level info --proxy-headers
in your service settings, does it says dockerfile? You can also try removing the dockerfile and using Railpack

Now it is working, check at https://back-email.felipe.fun/docs
I tested a small modification and now the system works, but I have another problem to solve, haha.
Mixed Content: The page at 'https://emails.felipe.fun/' was loaded over HTTPS, but requested an insecure resource 'http://back-email.felipe.fun/text'. This request has been blocked; the content must be served over HTTPS.
I appreciate all your effort in helping me, you don't know how much of a difference you made in my day and in resolving the original problem
I still have the same problem, I'll change the URLs to railway standard just to test and I'll get back to you
I tested some things but couldn't solve it, and within Insomnia I can only execute requests to the backend with https, so it's strange that it doesn't identify this
Also you could try using cloudflare in proxied, and enable the always https config
I changed the cloudflare configuration as you sent, and changed it to proxy mode
I imagine haha, my experience developing was cool but DevOps was never my thing
Now I need to run to record an explanation video, thank you very much for everything! I think there's a reward for the help, it's definitely yours haha, have a great night!
Have a great night as well! If you have any future issues with fastapi, you can tag me and I'll be happy to help
Status changed to Solved brody • 11 months ago






