简体   繁体   English

FastAPI - 必填字段,缺少值

[英]FastAPI - Field required, missing value

I receive the error below, when I deploy my app to Heroku:当我将我的应用程序部署到 Heroku 时,我收到以下错误:

{
   "detail": [
      {
         "loc": [
               "body",
               "id"
         ],
         "msg": "field required",
         "type": "value_error.missing"
      }
   ]
}

and log in the console:并登录控制台:

22-03-05T19:45:12.863425+00:00 heroku[router]: at=info method=GET path="/" host=wallet-reputation.herokuapp.com request_id=51780a1f-491a-4dd6-a8c0-164b41745405 fwd="95.175.20.47" dyno=web.1 connect=0ms service=4ms status=422 bytes=248 protocol=http

This is my Fastapi app:这是我的 Fastapi 应用程序:

@app.get("/")
def index(request: Request, id: str = Form(...)):
    return templates.TemplateResponse("main_page.html", context={"request": request})

That's how my HTML file looks like:这就是我的 HTML 文件的样子:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.1.3/dist/css/bootstrap.min.css"
          integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

    <title>Main Page</title>
  </head>
  <body>
    <form method="get">
      <center>
      <div class="form-group col-lg-4">
        <label for="walletAdress">Natluk Coin Reputation Wallet</label>
        <input type="text" class="form-control" id="walletAdress" aria-describedby="walletlHelp"
               placeholder="Wallet address" style="text-align: center" name="id">
      </div>
      </center>
      <center>
        <a class="btn btn-primary" type="submit" role="button"
           href="https://wallet-reputation.herokuapp.com/wallet/{{ id }}">Check Wallet</a>
        <a class="btn btn-primary" type="submit" role="button"
           href="https://wallet-reputation.herokuapp.com/wallet/run/{{ id }}">Create Wallet</a>
      </center>
</html>

So the logic is that, when I access my "/" endpoint I post the string into form, and then I click one of the buttons, which starts running the endpoint with variable from form.所以逻辑是,当我访问我的"/"端点时,我将字符串发布到表单中,然后我单击其中一个按钮,它开始使用来自表单的变量运行端点。

Update更新

Below is an example of how to submit a value through form, and how your endpoint should look like:下面是一个示例,说明如何通过表单提交值,以及您的端点应该是什么样子:

from fastapi import FastAPI, Request, status, Form
from fastapi.responses import HTMLResponse

app = FastAPI()

@app.get('/', response_class=HTMLResponse)
async def event_create_form(request: Request):
    html_content = """
    <html>
       <body>
          <form method="POST" action="/submit">
             <input type="text" name="id">
             <input type="submit" value="Create Event">
          </form>
       </body>
    </html>
    """
    return HTMLResponse(content=html_content, status_code=200)
    
@app.post('/submit')
async def event_create(id: int = Form(...)):
    return {"id": id}

Original answer原答案

You are submitting id as path parameter, but your endpoint expects it to be a body ( Form ) parameter.您正在提交id作为path参数,但您的端点希望它是一个 body ( Form ) 参数。 Thus, you should change your endpoint as shown below, so that id is expected as path parameter:因此,您应该如下所示更改端点,以便将id作为路径参数:

@app.get("/{id}")
def index(request: Request, id: str):
    ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FastAPI POST - 错误 422 详细信息'': ( ( loc'':(body file msg'':field required'', type'': value_error missing))) - FastAPI POST - Error 422 detail'': ( ( loc'':(body file msg'':field required'', type'': value_error missing))) 嵌套 Pydantic Model 使用 FastAPI 返回错误:需要字段(type=value_error.missing) - Nested Pydantic Model return error with FastAPI : field required (type=value_error.missing) 使用带有 FastAPI 的 POST 时缺少值错误 - Value Error Missing when using POST with FastAPI __call__() 缺少 1 个必需的位置参数:App Engine 上的“发送”FastAPI - __call__() missing 1 required positional argument: 'send' FastAPI on App Engine FastAPI &amp; sqlAlchemy: TypeError: Array() missing 1 required positional argument: &#39;sequence&#39; - FastAPI & sqlAlchemy: TypeError: Array() missing 1 required positional argument: 'sequence' TypeError:post() 缺少 1 个必需的位置参数:FastApi 中的“路径”? - TypeError: post() missing 1 required positional argument: 'path' in FastApi? 使用环境时 FastAPI 中需要 Pydantic 验证错误字段 - Pydantic validation error field required in FastAPI while using environments Fastapi Pydantic 可选字段 - Fastapi Pydantic optional field 异常值:缺少1个必需的位置参数 - Exception Value: missing 1 required positional argument 如果我进行查询,为什么会得到 {"detail":[{"loc":["path","id"],"msg":"field required","type":"value_error.missing"}]}与参数? - Why am I getting {"detail":[{"loc":["path","id"],"msg":"field required","type":"value_error.missing"}]} if I made query with params?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM