import { oak } from "https://deno.land/x/hex@0.5.15/src/service/deps.ts";
const { FormDataReader } = oak;
An interface which provides an interface to access the fields of a
multipart/form-data
body.
Normally an instance of this is accessed when handling a request body, and
dealing with decoding it. There are options that can be set when attempting
to read a multi-part body (see: FormDataReadOptions
).
If you .read()
the value, then a promise is provided of type
FormDataBody
. If you use the .stream()
property, it is an async
iterator which yields up a tuple of with the first element being a
Examples
Using .read()
:
import { Application } from "https://deno.land/x/oak/mod.ts";
const app = new Application();
app.use(async (ctx) => {
const body = ctx.request.body();
if (body.type === "form-data") {
const value = body.value;
const formData = await value.read();
// the form data is fully available
}
});
Using .stream()
:
import { Application } from "https://deno.land/x/oak/mod.ts";
const app = new Application();
app.use(async (ctx) => {
const body = ctx.request.body();
if (body.type === "form-data") {
const value = body.value;
for await (const [name, value] of value.stream()) {
// asynchronously iterate each part of the body
}
}
});
Methods
Reads the multipart body of the response and resolves with an object which contains fields and files that were part of the response.
Note: this method handles multiple files with the same name
attribute
in the request, but by design it does not handle multiple fields that share
the same name
. If you expect the request body to contain multiple form
data fields with the same name, it is better to use the .stream()
method
which will iterate over each form data field individually.
Returns an iterator which will asynchronously yield each part of the form
data. The yielded value is a tuple, where the first element is the name
of the part and the second element is a string
or a FormDataFile
object.