URL-Encoded Form Data

Avatar of Hemanta SundarayHemanta Sundaray

HttpClientRequest.bodyUrlParams() sends data as URL-encoded form data (application/x-www-form-urlencoded).

This is the same format browsers use for simple HTML forms. It’s the right choice for login endpoints, search forms, or any API that expects key-value pairs.

http.ts
import {
FetchHttpClient,
HttpClient,
HttpClientRequest,
} from "effect/unstable/http";
import { Effect } from "effect";
function login(username: string, password: string) {
return Effect.gen(function* () {
const client = yield* HttpClient.HttpClient;
const request = HttpClientRequest.post("https://httpbin.org/post").pipe(
HttpClientRequest.bodyUrlParams({
username,
password,
grant_type: "password",
}),
);
const response = yield* client.execute(request);
const data = yield* response.json;
return data;
}).pipe(Effect.provide(FetchHttpClient.layer));
}
// Test it
Effect.runPromise(login("emilys", "secret123")).then((data) => {
console.log("Content-Type sent:", data.headers["Content-Type"]);
console.log("Form data received:", data.form);
});

Output:

Terminal
Content-Type sent: application/x-www-form-urlencoded
Form data received: { grant_type: 'password', password: 'secret123', username: 'emilys' }

We’re using httpbin.org/post here because it echoes back everything it receives: the headers, form data, files, and raw body. This lets us verify exactly what was sent.

bodyUrlParams accepts the same input format as setUrlParams: an object, an array of tuples, or a URLSearchParams instance. It sets the Content-Type header to application/x-www-form-urlencoded automatically.

Sign in to save progress

Stay in the loop

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