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-urlencodedheaders: key-value pair- ถ้า
bodyเป็น FormData n8n จะเพิ่มcontent-type: multipart/form-dataให้อัตโนมัติ - ถ้า
bodyเป็นURLSearchParamsn8n จะเพิ่ม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]=cbrackets:{ a: ['b', 'c'] }เป็นa[]=b&a[]=crepeat:{ a: ['b', 'c'] }เป็นa=b&a=ccomma:{ a: ['b', 'c'] }เป็นa=b,c
auth: ใช้สำหรับ Basic auth ให้ใส่usernameและpasswordn8n แนะนำให้ใช้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: arrayBufferrejectUnauthorized: falseต้องเปลี่ยนเป็นskipSslCertificateValidation: true- ใช้
bodyให้ตรงกับcontent-typeheader เพื่อความชัดเจน resolveWithFullResponseเปลี่ยนเป็นreturnFullResponseและมีพฤติกรรมคล้ายกัน