Reading Body
ServerRequest.body
implements Deno.Reader
. And also implemnts utility mixin methods to read body data.
req.text()
parses UTF-8 decoded stringsreq.json()
try to parse body string as JSONreq.formData()
try to parse if content-type is one of:multipart/form-data
application/x-www-form-urlencoded
req.arryBuffer()
returns full raw body asUint8Array
import { contentTypeFilter, createApp } from "https://deno.land/x/[email protected]/mod.ts";
const app = createApp();
app.post("/json", contentTypeFilter("application/json"), async (req) => {
const bodyJson = (await req.json()) as { name: string; id: string };
// ...respond
});
app.post("/text", contentTypeFilter("text/plain"), async (req) => {
const bodyText = await req.text();
// ...respond
});
app.post(
"/multipart",
contentTypeFilter("multipart/form-data"),
async (req) => {
const bodyForm = await req.formData();
const name = bodyForm.value("name");
const file = bodyForm.file("file");
try {
// ...respond
} finally {
// Clean up stored temp files
await bodyForm.removeAll();
}
},
);
app.post(
"/form-urlencoded",
contentTypeFilter("application/x-www-form-urlencoded"),
async (req) => {
const bodyForm = await req.formData();
const name = bodyForm.value("name");
const id = bodyForm.value("id");
// ...respond
},
);
app.post("/raw", async (req) => {
const buf = await req.arrayBuffer();
// ...respond
});
// Start listening on port 8899
app.listen({ port: 8899 });