I’ve been having trouble switching from the HTTP package to using fetch for sending messages with the Mailgun API. The previous code using the HTTP package worked flawlessly:
Unfortunately, this results in a 400 Bad Request error. The Mailgun API seems to require form data instead of JSON, which the old HTTP package handled automatically but fetch does not. I found that converting the JSON data to form data resolves the issue:
Hit this exact same issue when updating our Meteor app. Mailgun’s API wants form-encoded data, and the HTTP package handled that conversion automatically with the params option. With fetch, you’ve got to do the encoding yourself.
Watch out for double-encoding - I wasted hours on a 400 error because I was JSON.stringify-ing data that was already formatted correctly. If you’ve got special characters in your email content, URLSearchParams usually handles encoding better than building forms manually.
Your auth looks right in the fetch version, so it’s definitely the form data encoding. Either FormData or URLSearchParams will work - just pick whatever fits your data better.
yeah, mailgun’s picky about form data vs json. you can also use urlsearchparams instead of multipartformdata - just do new urlsearchparams(params) and set content-type to application/x-www-form-urlencoded. works fine for basic text emails without attachments.
Had the same issue switching from HTTP package. Mailgun wants form-encoded data, not JSON. Skip the custom multipart library - just use the built-in FormData constructor:
Don’t set Content-Type manually with FormData - the browser handles it automatically with the right boundary. Plus this works with file attachments if you need them later.