# Fix FastAPI field required and value_error.missing Errors

> Resolve FastAPI 422 validation errors by sending JSON data instead of form data with requests.post(), or by modifying the endpoint to accept form input

**URL:** https://sentry.io/answers/solve-fastapi-field-required-and-value-error-missing-errors/

---

## The Problem

I have a Python script that sends a POST request to an API I've built using FastAPI. The POST request fails with error code 422 and the following details:

```json
{
  "detail": [
    {
      "loc": [ "body", "firstname" ],
      "msg": "field required",
      "type": "value_error.missing"
    },
    {
      "loc": [ "body", "lastname" ],
      "msg": "field required",
      "type": "value_error.missing"
    },
    {
      "loc": [ "body", "age" ],
      "msg": "field required",
      "type": "value_error.missing"
    }
  ]
}
```

My client script looks like this:

```python
import requests

response = requests.post('http://localhost:8000/create-user/',
                           data={
                              "firstname": "Robert",
                              "surname": "De Niro",
                              "age": 80
                           })
print("Response Status Code:", response.status_code)
print(response.json())
```

The relevant API endpoint and Pydantic model look like this:

```python
from pydantic import BaseModel
from fastapi import FastAPI

app = FastAPI()

class User(BaseModel):
    firstname: str
    surname: str
    age: int

@app.post("/create-user/")
async def create_users(user: User):
    # ... user creation operations ...
    return {"Message": "User created."}
```

What's going wrong? Is it a problem with my client script or my API code?

## The Solution

The problem is in the client script. An argument passed to the `data` parameter of [`requests.post`](https://requests.readthedocs.io/en/latest/api/#requests.post) will be submitted as form data, but the API expects JSON – this is why the error message seems to indicate that no data was received.

We can solve this by using the `json` parameter instead of the `data` parameter in our function call:

```python
import requests

response = requests.post('http://localhost:8000/create-user/',
                           json={
                              "firstname": "Robert",
                              "surname": "De Niro",
                              "age": 80
                           })
print(response.json())
```

This small change will cause `requests` to send our dictionary as JSON data, which should make our client and API endpoint work together as expected.

Alternatively, we could modify the FastAPI endpoint to accept form data, but this requires a few more code modifications. Following the [FastAPI documentation](https://fastapi.tiangolo.com/tutorial/request-forms/), we could install [`python-multipart`](https://pypi.org/project/python-multipart/):

```bash
pip install python-multipart
```

Then, we can modify the FastAPI endpoint to accept form data:

```python
from pydantic import BaseModel
from fastapi import FastAPI, Form # also import Form
from typing import Annotated # new import

app = FastAPI()

@app.post("/create-user/")
async def create_users(firstname: Annotated[str, Form()], surname: Annotated[str, Form()], age: Annotated[int, Form()]):
    # ... user creation operations ...
    return {"Message": "User created."}
```

Note that instead of relying on the fields defined in our Pydantic model, we need to define each field we expect this endpoint to accept. This may be useful if we're building an endpoint that should only receive some of the fields in a particular model. It is also useful if we would like our endpoint to be used by a simple [HTML form](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form) rather than a client submitting JSON data.

For more about using `Annotated`, please see [this answer](https://sentry.io/answers/exclude-one-or-more-fields-in-pydantic-model-from-appearing-in-fastapi-responses/).

---

*Source: [sentry.io/answers/solve-fastapi-field-required-and-value-error-missing-errors/](https://sentry.io/answers/solve-fastapi-field-required-and-value-error-missing-errors/)*
