Validating Responses Without a Body

Avatar of Hemanta SundarayHemanta Sundaray

Some HTTP responses carry meaning entirely through their status code and headers, with no body at all. Examples include a 204 No Content after a successful delete, a 304 Not Modified with cache headers, or a 201 Created with a Location header pointing to the new resource.

HttpClientResponse.schemaNoBody() validates the status code and headers of these bodiless responses against a schema. It never reads the body.

http.ts
import {
FetchHttpClient,
HttpClient,
HttpClientRequest,
HttpClientResponse,
} from "effect/unstable/http";
import { Effect, Schema } from "effect";
// A schema for a successful delete response
const DeleteResponse = Schema.Struct({
status: Schema.Literal(200),
headers: Schema.Struct({
"content-type": Schema.String,
}),
});
function deleteProduct(productId: number) {
return Effect.gen(function* () {
const client = yield* HttpClient.HttpClient;
const request = HttpClientRequest.del(
`https://dummyjson.com/products/${productId}`,
);
const response = yield* client.execute(request);
// Validate status + headers without reading the body
const validated =
yield* HttpClientResponse.schemaNoBody(DeleteResponse)(response);
console.log("Status:", validated.status);
console.log("Content-Type:", validated.headers["content-type"]);
return validated;
}).pipe(Effect.provide(FetchHttpClient.layer));
}
// Test it
Effect.runPromise(deleteProduct(1)).then(
(result) => console.log("Delete confirmed with status", result.status),
(error) => console.error("Validation failed:", error.message),
);

Output:

Terminal
Status: 200
Content-Type: application/json; charset=utf-8
Delete confirmed with status 200

schemaNoBody constructs an object with { status, headers } from the response and validates it against your schema. The body is never touched. This means it works correctly even on responses that have no body at all (where response.json would fail with an error).

Sign in to save progress

Stay in the loop

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