Skip to content

CORS middleware not working !  #5263

@TomEverson

Description

@TomEverson

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from unicodedata import category
from fastapi import Depends, FastAPI
import schemas,models
from database import SessionLocal, engine
from sqlalchemy.orm import Session
from typing import List, Optional
from fastapi.middleware import Middleware
from fastapi.middleware.cors import CORSMiddleware

middleware = [
    Middleware(
        CORSMiddleware,
        allow_origins=['*'],
        allow_credentials=True,
        allow_methods=['*'],
        allow_headers=['*']
    )
]

app = FastAPI(middleware=middleware)

models.Base.metadata.create_all(engine)

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

@app.post('/blog',tags=['blogs'])
def create(request : schemas.Blogs, db : Session = Depends(get_db),):
    new_mail = models.Blogs(title=request.title,category = request.category, body=request.body, translate=request.translate, author = request.author, translator = request.translator, published = request.published, read = request.read, photo = request.photo)
    db.add(new_mail)
    db.commit()
    db.refresh(new_mail)
    return new_mail

@app.get('/blog', tags=['blogs'])
def all(db: Session = Depends(get_db)):
    blogs = db.query(models.Blogs).all()
    return blogs

@app.get('/blog/',tags=['blogs'])
def all( category: Optional[str] = None,db: Session = Depends(get_db)):
    blogs = db.query(models.Blogs).all()
    if not category:
        return blogs
    results = db.query(models.Blogs).filter(models.Blogs.category == category).all()
    return results

@app.get('/blog/{title}',tags=['blogs'])
def show(title,db: Session = Depends(get_db)):
    blogs = db.query(models.Blogs).filter(models.Blogs.title == title).first()
    return blogs

@app.delete('/blog/{title}',tags=['blogs'])
def delete(title,db: Session = Depends(get_db)):
    db.query(models.Blogs).filter(models.Blogs.title == title).delete(synchronize_session='evaluate')
    db.commit()
    return "deleted"

@app.put('/blog/{title}',tags=['blogs'])
def update(title,request : schemas.Blogs,db: Session = Depends(get_db)):
    db.query(models.Blogs).filter(models.Blogs.title == title).update(request.dict())
    db.commit()
    return "updated"

@app.post('/email',tags=['emails'])
def create(request : schemas.Emails, db : Session = Depends(get_db)):
    new_email = models.Emails(email=request.email)
    db.add(new_email)
    db.commit()
    db.refresh(new_email)
    return new_email

@app.get('/email',tags=['emails'])
def all(db: Session = Depends(get_db)):
    blogs = db.query(models.Emails).all()
    return blogs

Description

When I make a request from Frontend with axios and whenever I made a request, it is showing the CORS error. And I already have setup the CORS middleware in fastapi.

const createMail = (email) => { axios.post(emails, email) .then(response => { const addedEmail = response.data; console.log(`POST: user is added`, addedEmail); }) .catch(error => {console.log(error)}); };

Operating System

Windows

Operating System Details

No response

FastAPI Version

0.79.0

Python Version

3.10

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions