-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.js
63 lines (53 loc) · 2.18 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const Scrappey = require('.');
// Replace 'YOUR_API_KEY' with your Scrappey API key
const apiKey = 'YOUR_API_KEY';
// Create an instance of Scrappey
const scrappey = new Scrappey(apiKey);
function getQueryString(object) {
const queryString = Object.keys(object)
.map(key => encodeURIComponent(key) + '=' + encodeURIComponent(object[key]))
.join('&');
return queryString;
}
async function run() {
try {
// Create a session
const sessionRequest = await scrappey.createSession();
const { session } = sessionRequest;
console.log('Created Session:', session);
// Make a GET request
const getRequestResult = await scrappey.getRequest({
url: 'https://reqres.in/api/users',
session,
});
console.log('GET Request Result:', getRequestResult);
// Make a POST request using FormData
const postFormData = { username: 'user123', password: 'pass456' };
const postRequestResultForm = await scrappey.postRequest({
url: 'https://reqres.in/api/users',
postData: getQueryString(postFormData),
session
});
console.log('POST Request Result (FormData):', postRequestResultForm);
// Make a POST request using JSON data
const postJsonData = { email: 'user@example.com', password: 'pass123' };
const postRequestResultJson = await scrappey.postRequest({
url: 'https://reqres.in/api/users',
postData: JSON.stringify(postJsonData),
customHeaders: {
'Content-Type': 'application/json', // Optional. To avoid issues please still add if you send JSON Data.
// 'auth': 'token'
},
session,
// proxyCountry: "UnitedStates"
// & more!
});
console.log('POST Request Result (JSON):', postRequestResultJson);
// Manually destroy the session (automatically destroys after 4 minutes)
await scrappey.destroySession(session);
console.log('Session destroyed.');
} catch (error) {
console.error(error);
}
}
run();