HTTP request helper for node builders#
n8n มี helper สำหรับการทำ HTTP request ที่ยืดหยุ่นและช่วยลดความซับซ้อน
Programmatic style only
ข้อมูลในเอกสารนี้ใช้กับ node ที่สร้างแบบ programmatic เท่านั้น ไม่ใช้กับ declarative style node
Usage#
เรียก helper นี้ในฟังก์ชัน execute
1 2 3 4 5 6 7 8 9 |
|
options
เป็น object:
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 |
|
url
จำเป็นต้องมี ฟิลด์อื่นๆ เป็น optional method เริ่มต้นคือ GET
หมายเหตุเกี่ยวกับแต่ละฟิลด์:
body
: สามารถใช้ object ปกติสำหรับ JSON payload, buffer สำหรับ file upload, FormData สำหรับmultipart/form-data
และURLSearchParams
สำหรับapplication/x-www-form-urlencoded
headers
: key-value pair- ถ้า
body
เป็น FormData n8n จะเพิ่มcontent-type: multipart/form-data
ให้อัตโนมัติ - ถ้า
body
เป็นURLSearchParams
n8n จะเพิ่มcontent-type: application/x-www-form-urlencoded
ให้อัตโนมัติ - ถ้าต้องการ override ให้ตั้ง header
content-type
เอง
- ถ้า
arrayFormat
: ถ้า query string มี array เช่นconst qs = {IDs: [15,17]}
ค่านี้จะกำหนดรูปแบบindices
(default):{ a: ['b', 'c'] }
เป็นa[0]=b&a[1]=c
brackets
:{ a: ['b', 'c'] }
เป็นa[]=b&a[]=c
repeat
:{ a: ['b', 'c'] }
เป็นa=b&a=c
comma
:{ a: ['b', 'c'] }
เป็นa=b,c
auth
: ใช้สำหรับ Basic auth ให้ใส่username
และpassword
n8n แนะนำให้ใช้helpers.httpRequestWithAuthentication(...)
แทนdisableFollowRedirect
: โดยปกติ n8n จะตาม redirect ถ้าไม่ต้องการให้ตั้งค่านี้เป็น trueskipSslCertificateValidation
: ใช้สำหรับเรียก HTTPS ที่ไม่มี certificate ที่ถูกต้องreturnFullResponse
: ถ้าต้องการให้ return object ที่มีข้อมูลมากกว่า body เช่น{body: body, headers: object, statusCode: 200, statusMessage: 'OK'}
encoding
: n8n จะตรวจสอบ content type ให้อัตโนมัติ แต่สามารถระบุarrayBuffer
เพื่อรับ Buffer ที่สามารถอ่านและใช้งานต่อได้
Example#
ดูตัวอย่างได้ที่ Mattermost node
Deprecation of the previous helper#
helper แบบเก่าที่ใช้ this.helpers.request(options)
และ expose ไลบรารี request-promise
ถูกถอดออกในเวอร์ชัน 1
เพื่อให้เข้ากันได้ n8n จึงเปลี่ยนไปใช้ไลบรารี Axios
แบบโปร่งใส
ถ้าคุณเจอปัญหา สามารถแจ้งได้ที่ Community Forums หรือ GitHub
Migration guide to the new helper#
helper ตัวใหม่ robust ขึ้น ใช้งานง่ายขึ้น และไม่ผูกกับไลบรารีใดๆ
node ใหม่ควรใช้ helper ตัวนี้ทั้งหมด และควรพิจารณา migrate node เดิมด้วย ข้อควรระวังหลักๆ:
- ต้องใช้
url
ไม่รองรับuri
encoding: null
ต้องเปลี่ยนเป็นencoding: arrayBuffer
rejectUnauthorized: false
ต้องเปลี่ยนเป็นskipSslCertificateValidation: true
- ใช้
body
ให้ตรงกับcontent-type
header เพื่อความชัดเจน resolveWithFullResponse
เปลี่ยนเป็นreturnFullResponse
และมีพฤติกรรมคล้ายกัน