-
Hello, I'm trying out xpresso for a project in which I need to handle Github webhooks, which means I need to be able to take a request payload as I couldn't find any way to directly extract the payload as bytes, while also maintaining the possibility of extracting it as Json with Should I be using a middleware or something different to do the validation? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Oh, it seems |
Beta Was this translation helpful? Give feedback.
-
There's a couple different ways to extract a body as bytes:
Where the imports for the are: from typing import Annotated # or from typing_extensions import Annotated for py<3.9
from xpresso import UploadFile, File, FromFile, FromJson, Json, UploadFile, FromMultipart |
Beta Was this translation helpful? Give feedback.
-
Using class WebhookInput(BaseModel):
delivery: Annotated[str,
HeaderParam(alias="X-GitHub-Delivery")]
event: Annotated[str,
HeaderParam(alias="X-Github-Event")]
payload_bytes: Annotated[bytes,
File(consume=False)]
payload: Annotated[Mapping[str, Any],
Json(consume=False)]
signature: Annotated[Optional[str],
HeaderParam(alias="X-Hub-Signature-256")]
async def handle_webhook(input: WebhookInput) -> Any:
... |
Beta Was this translation helpful? Give feedback.
There's a couple different ways to extract a body as bytes:
FromFile[UploadFile]
: This will extract the entire request into anUploadFile
and consume the request body. Note that unlike FastAPI this will not be a multipart request (that's handled usingFromMultipart[SomeFormModel]
).FromFile[bytes]
: This will extract directly into bytes but is otherwise the same as above.FromFile[AsyncIterable[bytes]
: this will give you a handle to the body stream (same asRequest.stream()
)Annotated[bytes, File(consume=False)]
: this is probably what you want. It will extract the request body to bytes without consuming it. You could then use that withAnnotated[SomePydanticModel, Json(consume=False)]
to …