Send a body
Send JSON, form data, text or bytes, encoded with a schema or as is.
When a call creates or changes something, it sends a body: a todo as JSON,
a form, a file. You pass the value as body, and Effetch picks how to
send it from its type. Add an input schema, and the body is typed and
encoded by it too. This page shows both, and the content-type each kind
of body is sent with.
Send JSON
Pass a plain object as body, and Effetch sends it as JSON:
import * as from "effect/Schema";
import type { } from "@monarcode/effetch";
declare const : .;
const = .({
: .,
: .,
: .,
});
export const = .("/todos/:id", {
: { : 1 },
: { : true },
: ,
});The request goes out with content-type: application/json.
post, put, patch and del take a body. get, head and options
don't, and TypeScript rejects one there. With api.request, any method
takes one, but a body on a GET or HEAD request makes the call die (see
When the methods aren't enough).
Encode the body with a schema
Add an input schema, and body is typed by it and encoded with it before
it's sent as JSON:
import * as from "effect/Schema";
import type { } from "@monarcode/effetch";
declare const : .;
const = .({
: .,
: .,
: .,
});
const = .({ : ., : . });
export const = .("/todos", {
: ,
: { : "Write docs", : new () },
: ,
});In create, due is a Date in your code and an ISO string on the wire.
A body that doesn't match input, or that can't be serialized as JSON,
makes the call die: it fails as a defect (an Effect that dies, which you
don't catch in normal code), with a message that starts with
Invalid request body:. A body the call can't send is a bug in the calling
code, not something to handle at runtime. See
Logging and defects.
To send a typed body as a form instead, such as a file upload, see Upload files.
Send a form, text or bytes
Without input, Effetch sends the body according to its type. A
FormData you build yourself is sent as a form:
import type { } from "@monarcode/effetch";
declare const : .;
export const = (: ) => {
const = new ();
.("file", );
return .("/uploads", { : });
};The following table shows how each type of body is sent, checked in this order:
| Body | Sent as | content-type |
|---|---|---|
An Effect HttpBody | As it is | The body's own |
| A string | As it is | text/plain |
An ArrayBuffer, typed array or DataView | Its bytes | application/octet-stream |
A Blob or File | As it is | The blob's type, or none when its type is empty |
FormData | As it is | multipart/form-data, with a boundary the platform adds |
URLSearchParams | As a URL-encoded form | application/x-www-form-urlencoded |
| Anything else, such as an object, array or number | JSON | application/json |
A string is sent as text/plain, even when it holds JSON. So
body: JSON.stringify(todo) arrives as text, and a server that only reads
JSON may reject it. Pass the object itself, and Effetch serializes it.
Set the content type
A content-type header on the call wins over the body's. Use it to send a
string you serialized yourself as JSON, or a body in a format of your own:
import type { } from "@monarcode/effetch";
declare const : .;
declare const : string;
export const = .("/todos/1", {
: ,
: { "content-type": "application/json" },
});A FormData body is the exception. Its content-type has to carry the
boundary the platform picks, so Effetch removes any content-type set by
the call, the endpoint or the client, and lets the platform set it.
A content-type in the client's headers is dropped for every body (see
Client configuration).
What's next
- Upload files: send a typed body as
multipart/form-data. - Read the response: decode what the server sends back.
- Call options: look up every option a call takes.