Skip to content

Commit 4d6d432

Browse files
author
Uğur Özyılmazel
committed
Add extra headers feature
1 parent e8f74f3 commit 4d6d432

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,27 @@ $ cat /tmp/data.json | jq 'length'
123123
736
124124
```
125125

126+
You can also add extra `X-` headers to your request. Read more about http
127+
headers at [Mozilla](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers)’s website.
128+
129+
```javascript
130+
const promptapi = require('@promptapi/scraper-pkg')
131+
params = {}
132+
headers = {'X-Referer': 'https://www.google.com'}
133+
promptapi.scraper('https://pypi.org/classifiers/', params, headers=headers).then(result => {
134+
if(result.error){
135+
console.log(result.error)
136+
} else {
137+
console.log(result.data); // your scraped data...
138+
console.log(result.headers);
139+
console.log(result.url);
140+
141+
promptapi.save('/tmp/data.html', result.data) // save result
142+
}
143+
})
144+
145+
```
146+
126147
---
127148

128149
## Development

scraper.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const validParams = [
1111
'selector',
1212
]
1313

14-
function scraper(url, params, timeout=5000){
14+
function scraper(url, params, headers={}, timeout=5000){
1515
try {
1616
let vURL = new URL(url)
1717
} catch(e) {
@@ -31,14 +31,19 @@ function scraper(url, params, timeout=5000){
3131
})
3232

3333
let promptAPIEndPoint = 'https://api.promptapi.com/scraper'
34-
34+
35+
let defaultHeaders = {
36+
'apikey': apiKey
37+
}
38+
if(Object.keys(headers).length > 0 && headers.constructor === Object){
39+
Object.assign(defaultHeaders, headers)
40+
}
41+
3542
let config = {
3643
method: 'get',
3744
url: promptAPIEndPoint,
3845
params: needParams,
39-
headers: {
40-
'apikey': apiKey,
41-
},
46+
headers: defaultHeaders,
4247
timeout: timeout,
4348
};
4449
return axios(config).then(response => {

0 commit comments

Comments
 (0)