PUT Request

Avatar of Hemanta SundarayHemanta Sundaray

A PUT request tells the server to replace a resource entirely. You send the complete new version of the resource, and the server swaps out the old one. Any fields you don’t include are removed or reset to defaults.

http.ts
import {
FetchHttpClient,
HttpClient,
HttpClientRequest,
} from "effect/unstable/http";
import { Effect } from "effect";
function replaceProduct(
productId: number,
product: { title: string; price: number; category: string },
) {
return Effect.gen(function* () {
const client = yield* HttpClient.HttpClient;
const request = HttpClientRequest.put(
`https://dummyjson.com/products/${productId}`,
).pipe(HttpClientRequest.bodyJsonUnsafe(product));
const response = yield* client.execute(request);
const data = yield* response.json;
return data;
}).pipe(Effect.provide(FetchHttpClient.layer));
}
// Test it — replace product 1 with entirely new data
Effect.runPromise(
replaceProduct(1, {
title: "Mechanical Keyboard",
price: 199,
category: "electronics",
}),
).then((data) => {
console.log("Updated product:", data.title, `- $${data.price}`);
});

Output:

Terminal
Updated product: Mechanical Keyboard - $199

HttpClientRequest.put(url) creates a request with the PUT method. The rest of the pattern is identical to POST: attach a body and execute.

Sign in to save progress

Stay in the loop

Get notified when new chapters are added and when this course is complete.