PATCH Request

Avatar of Hemanta SundarayHemanta Sundaray

A PATCH request tells the server to update specific fields of a resource, leaving everything else unchanged. You only send the fields you want to modify.

http.ts
import {
FetchHttpClient,
HttpClient,
HttpClientRequest,
} from "effect/unstable/http";
import { Effect } from "effect";
function updateProductPrice(productId: number, newPrice: number) {
return Effect.gen(function* () {
const client = yield* HttpClient.HttpClient;
// Only send the field we want to change
const request = HttpClientRequest.patch(
`https://dummyjson.com/products/${productId}`,
).pipe(HttpClientRequest.bodyJsonUnsafe({ price: newPrice }));
const response = yield* client.execute(request);
const data = yield* response.json;
return data;
}).pipe(Effect.provide(FetchHttpClient.layer));
}
// Test it — update only the price of product 1
Effect.runPromise(updateProductPrice(1, 5.99)).then((data) => {
console.log("Product:", data.title);
console.log("New price:", `$${data.price}`);
});

Output:

Terminal
Product: Essence Mascara Lash Princess
New price: $5.99

HttpClientRequest.patch(url) creates a request with the PATCH method. The body contains only the fields to update.

Sign in to save progress

Stay in the loop

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