Cors problem

feliperos19
FREEOP

a month 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.

Solved$40 Bounty

0 Replies


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,
)

feliperos19
FREEOP

a month ago

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=["*"],
    )

my repo: https://github.com/FelipeRos19/BackendDesafioAutoU


feliperos19
FREEOP

a month ago

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


Your config looks fine 🤔


feliperos19
FREEOP

a month ago

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


feliperos19
FREEOP

a month ago

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.


Let’s try a couple things:

  1. Did you put the domain to listen to port 8000? You could also put ENV var PORT with 8000

  2. Unless you opted for the IPv4 feature flag, make your app listen to ipv6

    --bind "[::]:8000" \

feliperos19
FREEOP

a month ago

I set it to port 8000 in the domain and created a variable PORT = 8000, now I will try to point it to IPV6


oranuare
PRO

a month ago

Do you have a separate frontend for this project making those requests?


feliperos19
FREEOP

a month ago

yes I have a separate front end, this is the link: https://github.com/FelipeRos19/FrontendDesafioAutoU


feliperos19
FREEOP

a month ago

I tested this configuration and it didn't work, I'm checking gunicorn



oranuare
PRO

a month ago

I've had that issue before with FastApi and a vue frontend


feliperos19
FREEOP

a month ago

I did the test and I still have the same error.
I'm trying to modify the backend dockerfile


feliperos19
FREEOP

a month ago

at cloudflare I'm only keeping DNS


oranuare
PRO

a month ago

uvicorn app.main:app --host :: --port ${PORT:-8000} --workers ${WORKERS:-3} --log-level info --proxy-headers

I use this to run my fastapi projects


oranuare
PRO

a month ago

in your case would be without the app.


oranuare
PRO

a month ago

Only main:app


oranuare
PRO

a month ago

You can use proxy with cloudflare, just make sure to have it in full


feliperos19
FREEOP

a month ago

I'll test this now and get back to you.


oranuare
PRO

a month ago

1423469179289342000


oranuare
PRO

a month ago

not full strict, that won't work


feliperos19
FREEOP

a month ago

it was already in this mode, so I don't think the problem is with cloudflare, I will test the railway domain directly


oranuare
PRO

a month ago

I think you are getting an error deploying the backend, because the url is not working tho


oranuare
PRO

a month ago

if you visit your backend url with /docs, you get a 502


feliperos19
FREEOP

a month ago

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

1423470713557225500


oranuare
PRO

a month ago

What do you get in logs when you visit that url?


feliperos19
FREEOP

a month ago

1423471808794984400


feliperos19
FREEOP

a month ago

my first time using fastapi, this project is a technical challenge and definitely not my best performance haha


oranuare
PRO

a month ago

don't worry, it happens


oranuare
PRO

a month ago

Let me give you a full dockerfile to try out


oranuare
PRO

a month ago

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


oranuare
PRO

a month ago

Try this one


oranuare
PRO

a month ago

without the app.main, only main:app


oranuare
PRO

a month ago

in your service settings, does it says dockerfile? You can also try removing the dockerfile and using Railpack

1423473035033313300


oranuare
PRO

a month ago

Now it is working, check at https://back-email.felipe.fun/docs


feliperos19
FREEOP

a month ago

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


oranuare
PRO

a month ago

No problem! Let me see that one


oranuare
PRO

a month ago

Are you using cloudflare only dns? or did you change it to proxied


feliperos19
FREEOP

a month ago

and using in full mode

1423475609606295600
1423475609941578000


oranuare
PRO

a month ago

Try removing the Dockerfile from your frontend and use it with railpack


oranuare
PRO

a month ago

Honestly I haven't deployed a single file, so idk if that could be the problem


feliperos19
FREEOP

a month ago

I still have the same problem, I'll change the URLs to railway standard just to test and I'll get back to you


feliperos19
FREEOP

a month ago

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


oranuare
PRO

a month ago

Try removing the / that we added before to text in the frontend, just to check


oranuare
PRO

a month ago

Also you could try using cloudflare in proxied, and enable the always https config


oranuare
PRO

a month ago

Let me see what is the config actually called


oranuare
PRO

a month ago

Ok, this is the option in cloudflare

1423482472800190500


oranuare
PRO

a month ago

and in that same page, is this one

1423482622088315000


feliperos19
FREEOP

a month ago

ITS WORKSSSSSSS


feliperos19
FREEOP

a month ago

1423482974581559300


oranuare
PRO

a month ago

Nice! Great job haha


oranuare
PRO

a month ago

Did cloudflare solved it, or was the url?


feliperos19
FREEOP

a month ago

I changed the cloudflare configuration as you sent, and changed it to proxy mode


oranuare
PRO

a month ago

Excellent!


feliperos19
FREEOP

a month ago

Thank you so much for your help, you just saved my life!


oranuare
PRO

a month ago

No worries, happy to help


oranuare
PRO

a month ago

I use fastapi every day, so I've faced many issues haha


feliperos19
FREEOP

a month ago

I imagine haha, my experience developing was cool but DevOps was never my thing


feliperos19
FREEOP

a month ago

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!


oranuare
PRO

a month ago

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 about 1 month ago


Loading...
Cors problem - Railway Help Station