Logging Requests

Avatar of Hemanta SundarayHemanta Sundaray

HttpClient.tapRequest() runs a side effect on the request object before it’s sent. This is ideal for logging outgoing requests.

http.ts
import {
FetchHttpClient,
HttpClient,
HttpClientRequest,
} from "effect/unstable/http";
import { Effect } from "effect";
function fetchUserWithRequestLogging(userId: number) {
return Effect.gen(function* () {
const client = (yield* HttpClient.HttpClient).pipe(
HttpClient.tapRequest((request) =>
Effect.sync(() => {
console.log(`[Request] ${request.method} ${request.url}`);
console.log(`[Request] Headers:`, request.headers);
}),
),
);
const request = HttpClientRequest.get(
`https://dummyjson.com/users/${userId}`,
).pipe(HttpClientRequest.setHeader("X-Request-Id", "abc-123"));
const response = yield* client.execute(request);
const user = yield* response.json;
return user;
}).pipe(Effect.provide(FetchHttpClient.layer));
}
// Test it
Effect.runPromise(fetchUserWithRequestLogging(1)).then((user) => {
console.log("User:", user.firstName, user.lastName);
});

Output:

Terminal
[Request] GET https://dummyjson.com/users/1
[Request] Headers: { 'x-request-id': 'abc-123' }
User: Emily Johnson

tapRequest receives the fully built HttpClientRequest — including any headers, URL params, and body that were added — and runs your side effect before the request is sent.

Note that whatever you return from the tapRequest callback doesn’t modify the request. The request passes through unchanged. Only the side effect (like logging) runs.

Sign in to save progress

Stay in the loop

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