Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
import { Readable } from 'node:stream';
import type { HttpRequest, HttpRequestOptions } from '@crawlee/types';
import { applySearchParams } from './url.js';
/**
* Converts {@apilink HttpRequestOptions} to a {@apilink HttpRequest}.
*/
export function processHttpRequestOptions({
searchParams,
form,
json,
username,
password,
...request
}: HttpRequestOptions): HttpRequest {
const url = new URL(request.url);
const headers = new Headers(request.headers);
applySearchParams(url, searchParams);
if ([request.body, form, json].filter((value) => value !== undefined).length > 1) {
throw new Error('At most one of `body`, `form` and `json` may be specified in sendRequest arguments');
}
const body = (() => {
if (form !== undefined) {
return Readable.from(new URLSearchParams(form).toString());
}
if (json !== undefined) {
return Readable.from(JSON.stringify(json));
}
if (request.body !== undefined) {
return Readable.from(request.body);
}
return undefined;
})();
if (form !== undefined && !headers.has('content-type')) {
headers.set('content-type', 'application/x-www-form-urlencoded');
}
if (json !== undefined && !headers.has('content-type')) {
headers.set('content-type', 'application/json');
}
if (username !== undefined || password !== undefined) {
const encodedAuth = Buffer.from(`${username ?? ''}:${password ?? ''}`).toString('base64');
headers.set('authorization', `Basic ${encodedAuth}`);
}
return { ...request, body, url, headers };
}