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.
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 itEffect.runPromise(login("emilys", "secret123")).then((data) => { console.log("Content-Type sent:", data.headers["Content-Type"]); console.log("Form data received:", data.form);});Output:
Content-Type sent: application/x-www-form-urlencodedForm 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.