You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Removes `HttpClient.stream()`, leaving only `HttpClient.sendRequest()`
(they were both returning a streamable `Response` since
#3295).
Extracts cookie- and redirect-related behaviour to a separate abstract
`BaseHttpClient` class.
Custom HTTP clients now only have to implement one `fetch` method
(matches the native `fetch` API). This makes the custom HTTP client
implementation easier and will hopefully drive community contributions.
Closes#3071Closes#3314
Blocked by apify/impit#348
The <ApiLinkto="basic-crawler/class/BasicCrawler">`BasicCrawler`</ApiLink> class allows you to configure the HTTP client implementation using the `httpClient` constructor option. This might be useful for testing or if you need to swap out the default implementation based on `got-scraping` for something else, such as `curl-impersonate` or `axios`.
13
+
The <ApiLinkto="basic-crawler/class/BasicCrawler">`BasicCrawler`</ApiLink> class allows you to configure the HTTP client implementation using the `httpClient` constructor option. This might be useful for testing or if you need to swap out the default implementation based on `got-scraping` for something else, such as `curl-impersonate`.
14
14
15
-
The HTTP client implementation needs to conform to the <ApiLinkto="types/interface/BaseHttpClient">`BaseHttpClient`</ApiLink> interface. For a rough idea on how it might look, see a skeleton implementation that uses the standard `fetch` interface:
15
+
## Built-in HTTP clients
16
+
17
+
Crawlee provides several HTTP client implementations out of the box:
18
+
19
+
-**`GotScrapingHttpClient`** (default) - Uses the `got-scraping` library for browser-like requests with support for custom headers, browser fingerprints, and proxies.
20
+
-**`ImpitHttpClient`** - Uses the `impit` library for making requests that closely mimic browser behavior.
21
+
-**`FetchHttpClient`** - Simple implementation using the native `fetch` API (does not support proxies).
22
+
23
+
## Implementing a custom HTTP client
24
+
25
+
To create a custom HTTP client, extend the <ApiLinkto="http-client/class/BaseHttpClient">`BaseHttpClient`</ApiLink> abstract class from `@crawlee/http-client`. The base class handles common functionality like cookie management, redirect following, session integration, proxy support, and timeout handling.
26
+
27
+
Your custom implementation only needs to override the `fetch` method to perform the actual network request:
By extending `BaseHttpClient`, your implementation automatically gets:
32
+
- Cookie jar management (applying cookies before requests, saving cookies from responses)
33
+
- Automatic redirect following (up to 10 redirects)
34
+
- Session integration (proxy URL and cookies from session)
35
+
- Timeout handling via AbortSignal
36
+
- Proxy URL support
37
+
19
38
You may then instantiate it and pass to a crawler constructor:
20
39
21
40
<CodeBlocklanguage="ts">{UsageSource}</CodeBlock>
22
41
23
-
Please note that the interface is experimental and it will likely change with Crawlee version 4.
42
+
Alternatively, you can implement the <ApiLinkto="types/interface/BaseHttpClient">`BaseHttpClient`</ApiLink> interface directly if you need full control over all aspects of the HTTP request handling, including cookies, redirects, and sessions. However, this approach requires implementing significantly more logic yourself.
* Register a function to be called at the very end of the request handling process. This is useful for resources that should be accessible to error handlers, for instance.
0 commit comments