The simplest way to send binary data is HttpClientRequest.bodyUint8Array(). It sets the request body to a raw byte array and optionally sets the Content-Type header.
This is useful when you already have the file contents in memory as a Uint8Array, for example, after reading a file, generating data programmatically, or receiving bytes from another source.
import { FetchHttpClient, HttpClient, HttpClientRequest,} from "effect/unstable/http";import { Effect } from "effect";
function sendBinaryData() { return Effect.gen(function* () { const client = yield* HttpClient.HttpClient;
// Create some binary data (in practice, this might come from a file) const encoder = new TextEncoder(); const bytes = encoder.encode("Hello from Effect!");
const request = HttpClientRequest.post("https://httpbin.org/post").pipe( HttpClientRequest.bodyUint8Array(bytes, "text/plain"), );
const response = yield* client.execute(request); const data = yield* response.json;
return data; }).pipe(Effect.provide(FetchHttpClient.layer));}
// Test itEffect.runPromise(sendBinaryData()).then((data) => { console.log("Content-Type sent:", data.headers["Content-Type"]); console.log("Body received:", data.data);});Output:
Content-Type sent: text/plainBody received: Hello from Effect!bodyUint8Array takes two arguments: the byte array and an optional content type string. If you omit the content type, it defaults to application/octet-stream (generic binary).
This API loads the entire payload into memory, so it’s best for small-to-medium payloads. For large files, use streaming instead.