Skip to Content
Calls

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:

BodySent ascontent-type
An Effect HttpBodyAs it isThe body's own
A stringAs it istext/plain
An ArrayBuffer, typed array or DataViewIts bytesapplication/octet-stream
A Blob or FileAs it isThe blob's type, or none when its type is empty
FormDataAs it ismultipart/form-data, with a boundary the platform adds
URLSearchParamsAs a URL-encoded formapplication/x-www-form-urlencoded
Anything else, such as an object, array or numberJSONapplication/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

On this page