Routes
Zeta automatically parses the request body into a useful type based on the Content-Type header.
The Content-Type header is required on all requests with a body. If it is not present, ctx.body will always be undefined, even when a body was sent.
This is the most common content type, and Zeta uses your validation library to validate the body before it reaches the route's handler.
;
;
Most of the time the body will be an object, but it can be any valid JSON value, like a number, string, array, boolean, etc.
WARNING
Devs forget to set theContent-Typeheader when usingfetchdirectly with a JSON body. If you forget, thebodywill beundefinedin the handler!
Zeta provides a util for parsing single file uploads: UploadFileBody. When used as the body schema, the body variable will be an instance of the File class.
;
;
On the client side, the body needs to be passed as a FormData object with a single key, file:
;
"file", file;
// No need to set the `Content-Type` header when using FormData
await "/upload", ;
Or you can use Zeta's built-in client which will build the FormData for you.
Similar to individual file uploads, Zeta provides a util for uploading FileLists: UploadFilesBody.
;
;
In the handler, instead of a FileList, you'll be provided a File[].
On the client side, the body is still FormData, but the key is files instead:
;
;
// No need to set the `Content-Type` header when using FormData
await "/upload", ;
And once again, you can use Zeta's built-in client which will build the FormData for you based on an input FileList.
To upload a generic FormData object, you can use the FormDataBody util.
;
;
If you need to upload another data type, like CSV, you can do so by adding the contentType metadata to your body schema:
;
;
Zeta can deserialize the following content types:
application/json → anymultipart/form-data → FormDatatext/* → stringapplication/x-www-form-urlencoded → FormDataapplication/octet-stream → BufferIf the content type is not supported, Zeta will throw an error.