When making POST, PUT, or PATCH requests, you typically need to send data in the request body.
The simplest way to send a JSON body is HttpClientRequest.bodyJsonUnsafe().
import { FetchHttpClient, HttpClient, HttpClientRequest,} from "effect/unstable/http";import { Effect } from "effect";
function addProduct(product: { title: string; price: number; category: string;}) { return Effect.gen(function* () { const client = yield* HttpClient.HttpClient;
const request = HttpClientRequest.post( "https://dummyjson.com/products/add", ).pipe(HttpClientRequest.bodyJsonUnsafe(product));
const response = yield* client.execute(request); const data = yield* response.json;
return data; }).pipe(Effect.provide(FetchHttpClient.layer));}
// Test itEffect.runPromise( addProduct({ title: "Ergonomic Keyboard", price: 149, category: "electronics", }),).then((data) => { console.log("Created product:", data);});Output:
Created product: { id: 195, title: 'Ergonomic Keyboard', price: 149, category: 'electronics'}bodyJsonUnsafe calls JSON.stringify internally and sets the Content-Type header to application/json for you.
The word “unsafe” here doesn’t mean dangerous. It means the serialization step is not tracked in the type system. If JSON.stringify were to fail at runtime (e.g., due to circular references in the data), the error would be an untracked exception rather than a typed Effect error.
This is the most convenient option when when you know the data is a simple, serializable object, like a hardcoded payload or a plain record you just constructed yourself.