You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js使用https.request调用Mailgun邮件接口时,auth对象引发Buffer.from()类型错误问题求助

Fixing the TypeError When Using auth with Node.js https.request()

Hey there! I totally get why this is confusing—let's break down what's causing that error and get your Mailgun email working properly.

Why You're Seeing the TypeError

The root issue here is how Node.js's native https.request() handles the auth option. Unlike some third-party HTTP libraries, the native module doesn't accept an object for auth. It expects a single string formatted as username:password instead.

When you pass {username: 'api', password: 'API_KEY'}, the internal Node.js code tries to process this object as if it were a string or buffer (in the Buffer.from() call you mentioned), which directly triggers that ERR_INVALID_ARG_TYPE error. And when you remove the auth object entirely, the request sends no credentials—hence the 401 "unauthorized" response from Mailgun.

The Fix: Use a String for auth

All you need to do is replace the auth object with a colon-separated string. Here's how to update your requestDetails object:

var requestDetails = {
 'protocol' : 'https:',
 'hostname' : 'api.mailgun.net',
 'method' : 'POST',
 'path' : '/v3/SANDBOXDOMAIN.mailgun.org/messages',
 'auth' : 'api:YOUR_ACTUAL_MAILGUN_API_KEY', // Replace with your real API key
 'headers' : {
 'Content-Type' : 'application/x-www-form-urlencoded',
 'Content-Length' : Buffer.byteLength(stringPayload)
 }
};

That's it! Now Node.js can properly encode the authentication credentials into the request's Authorization header, which Mailgun expects to validate your request.

Full Updated Code Snippet

To make it clear, here's the complete flow with the corrected auth field:

var querystring = require('querystring');
var https = require('https');

// Assuming orderData is defined in your code
var msg = 'Your payment of ' + (orderData.paymentIntent.amount / 100) + '$ for the order with order ID ' + orderData.id + ' has been received successfully';

var payload = {
 'from' : 'Mailgun Sandbox <postmaster@SANDBOXDOMAIN.mailgun.org>',
 'to' : 'MAILADDRESS',
 'subject' : 'Order Receipt For Order ID: ' + orderData.id,
 'html' : msg
};

var stringPayload = querystring.stringify(payload);

var requestDetails = {
 'protocol' : 'https:',
 'hostname' : 'api.mailgun.net',
 'method' : 'POST',
 'path' : '/v3/SANDBOXDOMAIN.mailgun.org/messages',
 'auth' : 'api:YOUR_ACTUAL_MAILGUN_API_KEY', // Updated to string format
 'headers' : {
 'Content-Type' : 'application/x-www-form-urlencoded',
 'Content-Length' : Buffer.byteLength(stringPayload)
 }
};

var req = https.request(requestDetails, function(res){
 var status = res.statusCode;
 if(status == 200) {
 callback(false);
 } else {
 callback('Status code returned was ' + status);
 }
});

// Don't forget to handle request errors and finalize the request
req.on('error', function(err) {
 callback(err);
});
req.write(stringPayload);
req.end();

Quick Bonus Tips

  • Double-check that you're using your real Mailgun API key (found in your Mailgun dashboard) in the auth string.
  • You can actually omit the Content-Length header if you want—Node.js will automatically calculate and set it for you when you call req.end(). Your original code is valid if you prefer to keep it, though.

内容的提问来源于stack exchange,提问作者rev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 07:53:13