Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add configuration option for additional request headers #96

Merged
merged 3 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ In the case [webhooks](#webhooks) are enabled, the webhook_payload option gives
`webhook_url`
The URL on which the webhook calls are made.

`additional_request_headers`
An object containing headers to be added to every request the crawler makes.
This can be useful to add authentication headers to crawl protected sites.

E.g. authenticate crawler with basic auth:
```
{
"additional_request_headers": {
"Authorization": "Basic dXNlcjpwYXNzd29yZA=="
}
}
```

## Webhooks

To be able to receive updates on the state of the crawler, you need to create a webhook. To do so, you absolutely need to have a public URL that can be reached by the crawler. This URL will be called by the crawler to send you updates.
Expand Down
20 changes: 20 additions & 0 deletions src/crawler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
PuppeteerCrawlingContext,
PuppeteerCrawlerOptions,
RequestQueue,
PuppeteerHook,
} from 'crawlee'

import { minimatch } from 'minimatch'
Expand Down Expand Up @@ -65,9 +66,28 @@ export class Crawler {
// type DefaultHandler = Parameters<typeof router.addDefaultHandler>[0];
router.addDefaultHandler(this.defaultHandler.bind(this))

const preNavigationHooks: PuppeteerHook[] = this.config
.additional_request_headers
? [
async (crawlingContext) => {
await crawlingContext.addInterceptRequestHandler(
async (request) => {
return await request.continue({
headers: {
...request.headers(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this would be an object instead of a function 🤔

https://docs.apify.com/sdk/js/docs/2.3/api/request#headers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case we're in a PuppeteerCrawlingContext, which means we have a Puppeteer HTTPRequest with a headers() method, see https://pptr.dev/api/puppeteer.httprequest.headers

...this.config.additional_request_headers,
},
})
}
)
},
]
: []

const puppeteerCrawlerOptions: PuppeteerCrawlerOptions = {
requestQueue,
requestHandler: router,
preNavigationHooks: preNavigationHooks,
launchContext: {
launchOptions: {
headless: this.config.headless || true,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type Config = {
meilisearch_api_key: string
start_urls: string[]
urls_to_exclude?: string[]
additional_request_headers?: Record<string, string>
queue?: string[]
primary_key?: string
batch_size?: number
Expand Down
Loading