

در این مقاله میآموزید چگونه در فرایند وب اسکریپینگ با NodeJS درخواستهای POST ارسال کنید. هدف عملی است: ارسال JSON، ارسال فرم (application/x-www-form-urlencoded)، مدیریت هدرها، و نکات پایداری و ضدمسدودسازی (rate limiting، پراکسی، user-agent و session). مثالها با کتابخانههای محبوب NodeJS یعنی Got، SuperAgent، node-fetch، Axios و request-promise آمدهاند و برای هر مثال توضیح ورودی، خروجی و گامهای اجرایی داده شده است.
یک درخواست POST که JSON ارسال میکند معمولاً شامل هدر Content-Type: application/json و بدنهٔ رشتهشدهٔ JSON است. برخی کتابخانهها (مثلاً Got با پارامتر json یا Axios) این تبدیل را خودکار انجام میدهند.
الگوی کلی:
import got from 'got';
const url = 'https://httpbin.org/post';
const data = { key: 'value' };
const options = {
json: data,
// got بهصورت خودکار Content-Type را application/json میگذارد
timeout: 5000,
};
got.post(url, options)
.then(response => {
console.log(response.body);
})
.catch(error => {
console.error(error);
});توضیح:
const request = require('superagent');
const url = 'https://httpbin.org/post';
const body = { key: 'value' };
request.post(url)
.send(body) // SuperAgent بهطور پیشفرض JSON را stringify میکند
.set('Content-Type', 'application/json')
.then(response => console.log(response.body))
.catch(err => console.error(err));توضیح:
import fetch from 'node-fetch';
const url = 'https://httpbin.org/post';
const data = { key: 'value' };
const options = {
method: 'POST',
body: JSON.stringify(data),
headers: { 'Content-Type': 'application/json' },
};
fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error(err));توضیح:
const axios = require('axios');
const uri = 'https://httpbin.org/post';
const data = { key: 'value' };
const options = { headers: { 'Content-Type': 'application/json' } };
axios.post(uri, data, options)
.then(response => console.log(response.data))
.catch(error => console.error(error));توضیح:
const request = require('request-promise');
const options = {
method: 'POST',
uri: 'https://httpbin.org/post',
body: { key: 'value' },
json: true, // اگر true باشد، کتابخانه body را stringify میکند و پاسخ را پارس میکند
headers: { 'Content-Type': 'application/json' }
};
request(options)
.then(response => console.log(response))
.catch(err => console.error(err));توضیح:
برای ارسال فرم معمولاً باید بدنه را به شکل query string بسازید (مثل key=value) و هدر Content-Type: application/x-www-form-urlencoded تنظیم شود. برخی کتابخانهها مثل Got پارامتر form دارند که این کار را ساده میکنند.
import got from 'got';
const url = 'https://httpbin.org/post';
const data = { key: 'value' };
const options = { form: data };
got.post(url, options)
.then(res => console.log(res.body))
.catch(err => console.error(err));توضیح:
const querystring = require('querystring');
const request = require('superagent');
const url = 'https://httpbin.org/post';
const data = { key: 'value' };
const body = querystring.stringify(data);
request.post(url)
.send(body)
.set('Content-Type', 'application/x-www-form-urlencoded')
.then(res => console.log(res.body))
.catch(err => console.error(err));توضیح:
import fetch from 'node-fetch';
import querystring from 'querystring';
const url = 'https://httpbin.org/post';
const data = { key: 'value' };
const options = {
method: 'POST',
body: querystring.stringify(data),
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
};
fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error(err));const axios = require('axios');
const uri = 'https://httpbin.org/post';
const data = 'key1=value1&key2=value2';
const options = { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } };
axios.post(uri, data, options)
.then(res => console.log(res.data))
.catch(err => console.error(err));const request = require('request-promise');
const querystring = require('querystring');
const options = {
method: 'POST',
uri: 'https://httpbin.org/post',
body: querystring.stringify({ key: 'value' }),
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
};
request(options)
.then(res => console.log(res))
.catch(err => console.error(err));برای افزایش throughput در اسکریپ کردن، میتوانید از همزمانی محدود استفاده کنید: مجموعهای از Promises را با محدودیت همزمانی اجرا کنید (مثلاً p-limit یا یک صف ساده). اجرای بیرویهٔ همزمان باعث سرریز منابع یا trigger شدن جلوگیری از طرف سرور میشود.
ارسال POST در جریان وب اسکریپینگ با NodeJS بسیار رایج است و کتابخانههای متعددی این کار را ساده میکنند. برای مقادیر JSON از پارامترهای خودکار (مثل json در Got یا Axios) استفاده کنید، برای فرمها از تبدیل به query string و تنظیم هدر مناسب بهره ببرید، و همیشه به مسائل پایداری و امنیت توجه کنید.
توصیه نهایی: برای پروژههای جدید از کتابخانههای مدرن مثل Got یا Axios استفاده کنید و request-promise را تنها در پروژههای legacy نگه دارید. هنگام اسکریپ کردن در مقیاس، مدیریت پراکسی، کوکی و نرخ درخواست را در اولویت قرار دهید تا اسکرپ پایدار بماند.


