英文:
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't include the request you're making in your question, it'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 != 'something':
raise HTTPException(status_code=401, detail="No no")
app = FastAPI(dependencies=[Depends(headercheck)])
@app.get("/item")
async def get_something(x: str):
return {'x': 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
{"detail":[{"loc":["header","xxx-access"],"msg":"field required","type":"value_error.missing"}]}
With the wrong authentication key:
$ curl -H "XXX-Access: something123" http://localhost:50001/item?x=foo
{"detail":"No no"}
And with the correct authentication key:
$ curl -H "XXX-Access: something" http://localhost:50001/item?x=foo
{"x":"foo"}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论