全局依赖在FastAPI中不起作用

huangapple go评论64阅读模式
英文:

Global dependency doesn't work in FastAPI

问题

dependecies.py

from fahttperrors import Fahttperror  # 这是一个 HTTP 异常类(不用担心)
from fastapi import Header

async def headercheck(xxx_access: str = Header(...)):

    if xxx_access != 'something':
        return Fahttperror.No_Header

main.py

from fastapi import FastAPI, Query, Depends
from dependencies import headercheck

app = FastAPI(dependencies=[Depends(headercheck)])

@app.get("/item")
async def get_something(x: str):
    return {'x': x,}

问题在于,无论我输入的是“something”还是其他内容,我始终会得到结果。为什么会这样?

英文:
dependecies.py

from fahttperrors import Fahttperror #this is http exception class (don't worry)
from fastapi import Header

async def headercheck(xxx_access: str = Header(...)):
    
    if xxx_access != 'something':
        return Fahttperror.No_Header

main.py

from fastapi import FastAPI, Query, Depends
from dependencies import headercheck


app = FastAPI(dependencies=[Depends(headercheck)])

@app.get("/item")
async def get_something(x: str):
    return {'x': x,}

The problem is I always get back result, if I write input "something" or not. Why is this?

答案1

得分: 2

作为答案也发一下。问题在于您的依赖返回了异常,但要真正停止请求,需要引发异常。只需将 headercheck 更改为:

async def headercheck(xxx_access: str = Header(...)):
    if xxx_access != 'something':
        raise Fahttperror.No_Header

应该解决问题。

英文:

Posting as an answer as well. The issue is that your dependency is returning the exception, but to actually stop the request, it needs to be raised. Simply changing headercheck to

async def headercheck(xxx_access: str = Header(...)):
    if xxx_access != 'something':
        raise Fahttperror.No_Header

should resolve the issue.

答案2

得分: 1

你的示例按预期运行 - 但由于你在问题中没有包括你所做的请求很难说你期望发生什么

从终端运行 `curl` 测试我们可以看到预期的行为首先没有头部信息

```bash
$ curl http://localhost:50001/item?x=foo
{ "detail":[ { "loc":[ "header","xxx-access" ],"msg":"field required","type":"value_error.missing" } ] }

使用错误的认证密钥:

$ curl -H "XXX-Access: something123" http://localhost:50001/item?x=foo
{ "detail":"No no" }

使用正确的认证密钥:

$ curl -H "XXX-Access: something" http://localhost:50001/item?x=foo
{ "x":"foo" }

<details>
<summary>英文:</summary>

Your example works as it should - but since you didn&#39;t include the request you&#39;re making in your question, it&#39;s hard to say what you expected to happen:

```python
from fastapi.exceptions import HTTPException
from fastapi import FastAPI, Query, Depends, Header

async def headercheck(xxx_access: str = Header(...)):
    if xxx_access != &#39;something&#39;:
        raise HTTPException(status_code=401, detail=&quot;No no&quot;)


app = FastAPI(dependencies=[Depends(headercheck)])

@app.get(&quot;/item&quot;)
async def get_something(x: str):
    return {&#39;x&#39;: x,}

If we test it using curl, we see that we get the expected behavior. First, without the header:

$ curl http://localhost:50001/item?x=foo
{&quot;detail&quot;:[{&quot;loc&quot;:[&quot;header&quot;,&quot;xxx-access&quot;],&quot;msg&quot;:&quot;field required&quot;,&quot;type&quot;:&quot;value_error.missing&quot;}]}

With the wrong authentication key:

$ curl -H &quot;XXX-Access: something123&quot; http://localhost:50001/item?x=foo
{&quot;detail&quot;:&quot;No no&quot;}

And with the correct authentication key:

$ curl -H &quot;XXX-Access: something&quot; http://localhost:50001/item?x=foo
{&quot;x&quot;:&quot;foo&quot;}

huangapple
  • 本文由 发表于 2023年6月26日 03:12:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定