Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Always getting 442 Unprocessable entity #23

@einfachTobi

Description

@einfachTobi

I cannot get msgpack-asgi running with FastAPI. Any request with msgpack-bytes is returned with a "422 Unprocessable Entity" error. The following minimal example will show the problem:

from pathlib import Path
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
from msgpack_asgi import MessagePackMiddleware


class Foo(BaseModel):
    bar: int


app = FastAPI()
app.add_middleware(MessagePackMiddleware)


@app.post("/")
def index(thing: Foo):
    return thing


if __name__ == "__main__":
    uvicorn.run(f"{Path(__file__).stem}:app", host="0.0.0.0", port=5001, log_level="debug", reload=True)

The data is sent with the following snippet:

import requests
import msgpack

url = "http://127.0.0.1:5001/"
headers = {"content-type": "application/x-msgpack"}
data_raw = {"bar": 23}
data_packed = msgpack.packb(data_raw)

response_json = requests.post(url, json=data_raw)
response_msgpack = requests.post(url, data=data_packed, headers=headers)

Resulting in:

INFO:     127.0.0.1:54682 - "POST / HTTP/1.1" 200 OK
INFO:     127.0.0.1:54684 - "POST / HTTP/1.1" 422 Unprocessable Entity

So the data is accepted as json but refused as msgpack-bytes. May there be an incompatibility with newser versions of FastAPI or pydantic? Or am I just using this completely wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions