@marlen
Для отправки POST запроса с Node.js, вы можете использовать встроенный модуль http или установить более удобный модуль, такой как axios или node-fetch.
Пример отправки POST запроса с использованием модуля http:
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 |
const http = require('http');
const postData = JSON.stringify({
key: 'value'
});
const options = {
hostname: 'example.com',
port: 80,
path: '/endpoint',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData)
}
};
const req = http.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log(data);
});
});
req.on('error', (error) => {
console.error(error);
});
req.write(postData);
req.end();
|
Или же используя модуль axios:
1 2 3 4 5 6 7 8 9 10 11 |
const axios = require('axios');
axios.post('http://example.com/endpoint', {
key: 'value'
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
|
Пожалуйста, поменяйте параметры запроса (hostname, port, path, data и т. д.) под свои нужды.