HTTP Request Node: เชื่อมต่อ API อะไรก็ได้ด้วย n8n
คู่มือใช้งาน HTTP Request Node ใน n8n ครอบคลุม GET, POST, Authentication ทุกประเภท Headers, Body และการจัดการ Response
n8n มี Built-in Node สำหรับบริการยอดนิยมหลายร้อยตัว เช่น Google Sheets, Slack, LINE, Shopee แต่ในโลกจริงมักเจอ API ที่ไม่มี Node เฉพาะ — ระบบ ERP ของบริษัท, API ของ e-Commerce platform ไทย, หรือ Internal API ที่ทีมสร้างขึ้นเอง
HTTP Request Node แก้ปัญหานี้ทั้งหมด มันคือ Node ที่ส่ง HTTP Request ไปยัง URL ใดก็ได้ รองรับทุก Method ทุก Header ทุกประเภท Authentication
ความเข้าใจพื้นฐาน
HTTP Request Node ทำงานเหมือน Postman หรือ cURL แต่ฝังอยู่ใน Workflow — คุณกำหนด URL, Method, Headers, Body แล้ว Node จะส่ง Request และส่ง Response กลับมาให้ Node ถัดไปประมวลผลต่อ
การตั้งค่าหลัก:
- URL — ที่อยู่ปลายทาง รองรับ Expression เพื่อสร้าง URL แบบ dynamic
- Method — GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS
- Authentication — วิธีพิสูจน์ตัวตนกับ API
- Headers — HTTP Headers เพิ่มเติม
- Body — ข้อมูลที่ส่งไปกับ Request (สำหรับ POST/PUT/PATCH)
GET Request: ดึงข้อมูล
GET Request คือการขอข้อมูลจาก API ไม่มี Body สามารถส่ง Parameters ผ่าน Query String
ตัวอย่าง: ดึงสภาพอากาศ
URL: https://api.openweathermap.org/data/2.5/weather
Method: GET
Query Parameters:
q: Bangkok
appid: {{ $env.OPENWEATHER_API_KEY }}
units: metric
lang: th
n8n จะแปลง Query Parameters เป็น URL string อัตโนมัติ:
https://api.openweathermap.org/data/2.5/weather?q=Bangkok&appid=...&units=metric&lang=th
Dynamic URL: ถ้าต้องดึงข้อมูล User ทีละคน ใช้ Expression ใน URL:
URL: https://api.example.com/users/{{ $json.user_id }}
POST Request: ส่งข้อมูล
POST Request ใช้สร้างข้อมูลใหม่ มักต้องส่ง Body ด้วย
Body แบบ JSON:
Method: POST
URL: https://api.example.com/orders
Body Content Type: JSON
Body:
{
"customer_id": "{{ $json.customer_id }}",
"items": {{ $json.cart_items }},
"total": {{ $json.total }},
"note": "{{ $json.note }}"
}
Body แบบ Form Data:
บาง API รับข้อมูลแบบ application/x-www-form-urlencoded:
Body Content Type: Form URL Encoded
Fields:
grant_type: authorization_code
code: {{ $json.auth_code }}
client_id: {{ $env.CLIENT_ID }}
client_secret: {{ $env.CLIENT_SECRET }}
Authentication
1. API Key ใน Header
วิธีที่พบบ่อยที่สุด ส่ง Key ใน Header ชื่อที่ API กำหนด (มักเป็น Authorization หรือ X-API-Key)
ใน Authentication Tab เลือก “Header Auth”:
- Header Name:
X-API-Key - Header Value:
{{ $env.API_KEY }}
หรือ Basic Auth แบบ Bearer Token:
- Header Name:
Authorization - Header Value:
Bearer {{ $env.ACCESS_TOKEN }}
2. Basic Auth ส่ง username/password แบบ Base64 encoded เลือก “Basic Auth” ใน Authentication Tab แล้วใส่ Username และ Password
3. OAuth2 สำหรับ API ที่ใช้ OAuth2 n8n มีระบบ Credential สำหรับ OAuth2 โดยเฉพาะ ทำการตั้งค่า Client ID/Secret ครั้งเดียว แล้ว n8n จัดการ Token refresh อัตโนมัติ
4. Custom Header Auth บาง API ใช้ Auth แบบ Custom เช่น HMAC Signature คำนวณใน Code Node ก่อน แล้วส่งค่า signature ผ่าน Header
Headers ที่ใช้บ่อย
Content-Type: application/json
Accept: application/json
X-Request-ID: {{ $execution.id }}
User-Agent: n8n-workflow/1.0
การส่ง X-Request-ID ช่วย debug ได้ง่ายเมื่อ API ปลายทาง มี Log ที่บันทึก Request ID
จัดการ Response
HTTP Request Node ส่ง Response กลับมาในรูปแบบ:
{
"status": 200,
"headers": { ... },
"body": { /* JSON response */ }
}
ค่า default คือ Response Body จะถูก parse เป็น JSON อัตโนมัติ (ถ้า Content-Type เป็น JSON)
ตั้งค่า Response:
- Response Format — JSON (default), Text, File, Buffer
- Full Response — เปิดเพื่อรับ headers และ status code ด้วย ไม่ใช่แค่ body
ควรเปิด Full Response เมื่อต้องการตรวจ status code:
// ใน Code Node ถัดมา
const status = $input.item.json.statusCode;
if (status === 200) {
// สำเร็จ
} else if (status === 404) {
// ไม่พบข้อมูล
}
Options ที่สำคัญ
Timeout — กำหนดเวลา timeout เป็น milliseconds (default ไม่มี timeout) แนะนำตั้งไว้ เช่น 30000 (30 วินาที)
Redirect — Follow Redirects เปิดไว้ default แต่ปิดได้ถ้า API ส่ง redirect เป็นส่วนหนึ่งของ response
SSL Certificate — ปิดการ verify SSL ได้ (ใช้เฉพาะกับ internal API ที่ใช้ self-signed cert เท่านั้น)
Batching — สำหรับ Node ที่ได้รับ Items หลายตัว สามารถตั้งค่าให้ส่ง Request ทีละ N Items พร้อมกัน และหน่วง Delay ระหว่าง Batch เพื่อป้องกัน Rate Limit
ตัวอย่างจริง: เรียก LINE Messaging API
LINE Messaging API ไม่มี Native Node ใน n8n (ที่รองรับทุก feature) ใช้ HTTP Request Node แทน:
Method: POST
URL: https://api.line.me/v2/bot/message/push
Headers:
Authorization: Bearer {{ $env.LINE_CHANNEL_ACCESS_TOKEN }}
Content-Type: application/json
Body (JSON):
{
"to": "{{ $json.line_user_id }}",
"messages": [
{
"type": "text",
"text": "สวัสดี {{ $json.name }}! ออเดอร์ของคุณพร้อมส่งแล้ว"
}
]
}
ตัวอย่างจริง: เรียก Internal API ที่ใช้ HMAC Auth
// Code Node ก่อน HTTP Request — สร้าง signature
const crypto = require('crypto');
const timestamp = Date.now().toString();
const payload = JSON.stringify($input.item.json);
const signature = crypto
.createHmac('sha256', $env.API_SECRET)
.update(`${timestamp}.${payload}`)
.digest('hex');
return {
...$input.item.json,
_timestamp: timestamp,
_signature: signature
};
// HTTP Request Node
Method: POST
URL: https://internal-api.company.com/process
Headers:
X-Timestamp: {{ $json._timestamp }}
X-Signature: {{ $json._signature }}
Content-Type: application/json
Debug HTTP Request
เมื่อ API ส่ง Error กลับมา ให้เปิด Full Response เพื่อดู status code และ response body ทั้งหมด บ่อยครั้ง API ส่งรายละเอียด error ใน body แต่ถ้า Follow JSON response เฉยๆ อาจไม่เห็น
ใช้ “Test Step” เพื่อ run เฉพาะ HTTP Request Node และดู response โดยไม่ต้องรัน Workflow ทั้งหมด ประหยัดเวลา debug มาก
HTTP Request Node เป็นหนึ่งใน Node ที่ใช้บ่อยที่สุดใน n8n ยิ่งเข้าใจมันลึก ยิ่งสามารถเชื่อมต่อกับระบบที่หลากหลายได้ และนั่นคือสิ่งที่ทำให้ n8n แตกต่างจากเครื่องมือ Automation ทั่วไป
อยากเรียน n8n แบบเป็นระบบ ตั้งแต่เริ่มต้นจนสร้าง Workflow ใช้งานจริงได้ ลองดู คอร์สสอน n8n ที่ aiunlock.co
Related posts
จัดการ API Pagination ใน n8n: ดึงข้อมูลครบทุกหน้า
เรียนรู้วิธีจัดการ API Pagination ทุกรูปแบบใน n8n ตั้งแต่ Page-based, Cursor-based จนถึง Link Header เพื่อดึงข้อมูลครบทุก Record
n8n Advanced: 10 เทคนิคขั้นสูงสำหรับมืออาชีพ
รวม 10 เทคนิค n8n ขั้นสูงที่มืออาชีพใช้จริง ตั้งแต่ Error Handling, Sub-Workflow, Code Node ไปจนถึง API Pagination
สร้าง AI Agent ด้วย n8n: ให้ AI ทำงานแทนคุณ
วิธีสร้าง AI Agent ใน n8n ที่วางแผนและใช้ Tools เองได้ ตั้งแต่ตั้งค่าจนถึง Agent หลายขั้นตอนขั้นสูง