Read request body
Access the request body by calling methods on request.body. Methods validate
that the body is of the correct type.
// routes/api.ts
import route from "primate/route";
export default route({
async post(request) {
const received = await request.body.text();
return { received };
},
});// routes/api.ts
import route from "primate/route";
export default route({
async post(request) {
const received = await request.body.json();
return { received };
},
});
Form -> Record
Use form() to get the body as a form (Record<string, string>).
import route from "primate/route";
export default route({
async post(request) {
const received = await request.body.form();
return { received };
},
});form() keeps the last value when a field name occurs more than once. Use
formEntries() when repeated fields or their submission order matter. It
returns a JSON-compatible array of [name, value] pairs.
const entries = await request.body.formEntries();
const categoryIds = entries
.filter(([name]) => name === "categoryIds")
.map(([, value]) => value);import route from "primate/route";
export default route({
async post(request) {
const { form, files } = await request.body.multipart();
return { form };
},
});// routes/api.ts
import route from "primate/route";
export default route({
async post(request) {
const blob = await request.body.blob();
return { blob };
},
});