-
Notifications
You must be signed in to change notification settings - Fork 9
/
delete-request.js
60 lines (52 loc) · 1.4 KB
/
delete-request.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
const https = require('https');
function deleteRequest(path) {
const options = {
hostname: 'reqres.in',
// 👇 specify path e.g. /api/users/2
path: path,
method: 'DELETE',
port: 443, // 👈️ replace with 80 for HTTP requests
headers: {
'Content-Type': 'application/json',
},
};
return new Promise((resolve, reject) => {
const req = https.request(options, res => {
let rawData = '';
res.on('data', chunk => {
rawData += chunk;
});
res.on('end', () => {
try {
// 👇️ It's possible that your API has no response for DELETE requests
// If so, remove `JSON.parse(rawData)` and simply resolve()
resolve(JSON.parse(rawData));
} catch (err) {
reject(new Error(err));
}
});
});
req.on('error', err => {
reject(new Error(err));
});
req.end();
});
}
exports.handler = async event => {
try {
const result = await deleteRequest(`/api/users/2`);
console.log('result is: 👉️', result);
// 👇️️ response structure assume you use proxy integration with API gateway
return {
statusCode: 200,
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(result),
};
} catch (error) {
console.log('Error is: 👉️', error);
return {
statusCode: 400,
body: error.message,
};
}
};