Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global Dependencies doc example not working as-is #4978

Closed
9 tasks done
jerinpetergeorge opened this issue Jun 2, 2022 · 1 comment
Closed
9 tasks done

Global Dependencies doc example not working as-is #4978

jerinpetergeorge opened this issue Jun 2, 2022 · 1 comment
Labels
question Question or problem question-migrate

Comments

@jerinpetergeorge
Copy link

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

# code snippet copied from the official documentation

from fastapi import Depends, FastAPI, Header, HTTPException


async def verify_token(x_token: str = Header()):
    if x_token != "fake-super-secret-token":
        raise HTTPException(status_code=400, detail="X-Token header invalid")


async def verify_key(x_key: str = Header()):
    if x_key != "fake-super-secret-key":
        raise HTTPException(status_code=400, detail="X-Key header invalid")
    return x_key


app = FastAPI(dependencies=[Depends(verify_token), Depends(verify_key)])


@app.get("/items/")
async def read_items():
    return [{"item": "Portal Gun"}, {"item": "Plumbus"}]


@app.get("/users/")
async def read_users():
    return [{"username": "Rick"}, {"username": "Morty"}]

Description

TL;DR: maybe related to #4906

I was following the documentation of Global Dependencies and the example in the documentation is not working as expected.

I am getting the 422 status code with the following response

{
    "detail": [
        {
            "loc": [
                "header",
                "x-token"
            ],
            "msg": "field required",
            "type": "value_error.missing"
        },
        {
            "loc": [
                "header",
                "x-key"
            ],
            "msg": "field required",
            "type": "value_error.missing"
        }
    ]
}

Adding a default parameter to the Header(...) solved the issue

async def verify_token(x_token: str = Header(default=None)):
    ...


async def verify_key(x_key: str = Header(default=None)):
    ...

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.78.0

Python Version

3.8.10

Additional Context

No response

@jerinpetergeorge jerinpetergeorge added the question Question or problem label Jun 2, 2022
@odiseo0
Copy link

odiseo0 commented Jun 3, 2022

If you're not passing the X-Token or the X-Key header you'll get a 422 error because you're telling the client that those headers are required

@jerinpetergeorge jerinpetergeorge changed the title Global Dependencies not working Global Dependencies doc exampl enot working as-is Jun 4, 2022
@jerinpetergeorge jerinpetergeorge changed the title Global Dependencies doc exampl enot working as-is Global Dependencies doc example not working as-is Jun 4, 2022
@jerinpetergeorge jerinpetergeorge closed this as not planned Won't fix, can't repro, duplicate, stale Jun 4, 2022
@tiangolo tiangolo reopened this Feb 27, 2023
Repository owner locked and limited conversation to collaborators Feb 27, 2023
@tiangolo tiangolo converted this issue into discussion #6411 Feb 27, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

3 participants