I’m trying to send emails through Mailgun using XSJS but I’m running into issues when my HTML content exceeds about 2000 characters. My current implementation works fine for smaller HTML content but fails with larger files.
Here’s what I’m currently using:
var content = $.request.body.asString();
var mailRequest = new $.web.WebRequest($.net.http.POST, "/messages");
mailRequest.headers.set('Content-Type', encodeURIComponent("application/x-www-form-urlencoded"));
mailRequest.parameters.set("domain", "mydomain.mailgun.org");
mailRequest.parameters.set("from", "[email protected]");
mailRequest.parameters.set("to", '[email protected]');
mailRequest.parameters.set("subject", "Email Subject");
mailRequest.parameters.set("html", content);
The problem is that when I try sending larger HTML files, I get a “414 Request-URI Too Large” error. I think this happens because the data gets passed through the URL parameters instead of the request body. How can I modify my code to handle longer HTML content properly? I’m pretty new to XSJS so detailed explanations would be really helpful.
The problem is XSJS treats your parameters as GET query strings instead of POST body data. When you use mailRequest.parameters.set(), it just tacks everything onto the URL - that’s what’s causing your 414 error with large content.
I hit this same issue migrating old email templates through XSJS. You need to manually build the POST body as a formatted string, then use mailRequest.setBody() to attach it.
Your auth setup needs fixing too - you can’t pass API credentials through URL parameters with big payloads. Put your Mailgun API key in the Authorization header using Basic auth instead. Also make sure your Content-Type header is set right without any encoding.
This way everything goes in the request body instead of the URL, so you avoid those size limits completely.
classic xsjs/mailgun issue. you’re using setParameters which creates query strings - that’s what’s hitting the 414 limit. switch to setBody() with form encoding instead: “from=your@email&to=recip@email&html=” + your_html_content. drop the encodeURIComponent on the content-type header too - just use “application/x-www-form-urlencoded” straight up. fixed the same problem for me.
I encountered a similar issue with Mailgun when using XSJS. The 414 error arises because you’re trying to send too much data through the URL parameters, which is limited in size. To resolve this, you should send your HTML content in the request body instead. You can do this by constructing the POST body manually. For example, create a string like “[email protected]&[email protected]&subject=Your+Subject&html=” + encodeURIComponent(content). Then, use mailRequest.setBody() to set this string as the body of your request. Make sure to keep the Content-Type header appropriately set without using encodeURIComponent for it. This adjustment allows you to send large HTML emails without any issues.
Your Content-Type header is encoded wrong. Drop the encodeURIComponent wrapper and use form-data for large payloads instead. Right now you’re converting everything to query strings, which hits URL length limits. I ran into this same issue migrating old email systems. Build a proper multipart form request body instead of using parameters. Create the body content directly and set the right boundaries in your Content-Type header. Or just switch to Mailgun’s JSON API - it handles big payloads way better than the form endpoint. JSON fixes the URL encoding problems since everything goes in the request body. You’ll need to change your auth to use the Authorization header with your API key instead of passing credentials through form parameters.
That 414 error is from URL length limits when you’re passing large HTML content through parameters. Skip the XSJS headaches and manual body construction - there’s a better way to automate this whole email workflow.
I’ve hit similar email automation issues in production. The real problem isn’t just fixing this technical hiccup - it’s building a solid email system that handles different content sizes, retries, logging, and monitoring without you babysitting it.
What worked best for us was setting up an automated pipeline that treats email sending as a service. You can build this with Latenode, which connects straight to Mailgun’s API and sidesteps those XSJS limitations. It handles request body formatting automatically, retries failed sends, and scales with whatever content size you throw at it.
The automation route also gives you way better error handling and monitoring than cramming email logic into your app code. Plus you can easily tack on email templates, scheduling, or A/B testing down the road.
Been there with XSJS email automation. You’re building everything manually when you should automate the whole pipeline.
Sure, switching from parameters to setBody() fixes the 414 error, but now you’re stuck maintaining email logic in your app code. Gets messy fast when you need error handling, retries, templates, or different providers.
I had this exact problem and moved email sending to an external platform. Instead of wrestling with XSJS request formatting, set up a proper email service that handles the technical stuff automatically.
Latenode connects directly to Mailgun and processes any size HTML content without URL parameter headaches. Just send your data to the workflow and it handles API calls, formatting, retries, and monitoring.
Also makes it easy to add features later - templates, scheduling, or switching providers without touching your XSJS code.
Way cleaner than debugging manual HTTP requests every time Mailgun changes something or you hit another edge case.