When the methods aren't enough
Choose the method at runtime, read a stream, or use the HttpClient underneath a client.
The client's methods cover most calls, but a few needs fall outside them:
an HTTP method you only know at runtime, a response that arrives a piece at
a time, or a request that Effetch doesn't model at all. This page shows the
way out for each one: api.request, api.sse and api.ndjson, and the
HttpClient underneath the client.
Choose the method at runtime
When the method is only known at runtime, use api.request. It takes the
method as its first argument, then the path and the same options as the
other methods:
import type * as from "effect/http/HttpMethod";
import type { } from "@monarcode/effetch";
declare const : .;
export const = (: ., : string) =>
.(, );api.request also sends the methods that have no shorthand, such as
TRACE or QUERY. The other methods are shorthand for it.
api.request types body the same way for every method, a literal
"GET" included, so TypeScript doesn't stop a body on a GET. When the
call runs, a body on a GET or HEAD request makes it die, since those
requests can't carry one.
Read a stream of events or lines
When the response is a stream of Server-Sent Events or newline-delimited
JSON, use api.sse or api.ndjson. Each gives you an Effect Stream of
decoded items. See Streaming responses.
Use the underlying HttpClient
When you need something the methods don't do, such as reading a response
your own way, use api.httpClient. It's the configured Effect HttpClient
underneath the client, and it takes Effect's own requests:
import * as from "effect/Effect";
import * as from "effect/http/HttpClientRequest";
import type { } from "@monarcode/effetch";
declare const : .;
// Reads the export as text, whatever its status.
export const = .(
..(.("/export.csv")),
() => .,
);api.httpClient adds the base URL and the client's default headers to each
request, and runs every plugin with its default options. Transport failures
fail with NetworkError, next to the plugins' own errors.
It leaves out the rest of what a call does:
- No status check. A
404succeeds with its response, so checkresponse.statusyourself. - No decoding. There's no
output, noAcceptheader picked for you, and noDecodeError. - No error schemas. The client's
defaultErrorandstatusErrorsdon't apply. - No
onErrororonExithooks. A plugin that works from those, such asconsoleLogger, which logs fromonExit, isn't run for these requests. - No per-call plugin options. You can't turn a plugin off, or change its options, for one request.
For a streamed body, use api.sse or api.ndjson rather than
api.httpClient.
What's next
- Streaming responses: read Server-Sent Events and newline-delimited JSON.
- Call errors: see what can go wrong in a call.
- Call options: look up every option the methods take.