Ensure Correct Content-type for XHR2 FormData

Just a heads up that if you’re submitting data via FormData with XHR2, you’ll need to ensure the proper Content-type header is set, otherwise request data won’t be sent properly. In my case, an incorrect Content-type header resulted in only sending FormData’s first attribute with the request.

Here’s some detail.

This was the form in question; Signal’s login form code, note the email and password fields:

Login form markup

Login form markup

Straightforward enough. But here’s what an XHR2 request looked like in Chrome’s Dev Tools:

XHR2 request, only sending the form's first element value.

XHR2 request, only sending the form’s first element value.

Note that only the email value is being sent. Something is off.

Turns out the Content-type request header was set incorrectly. As shown above, it was set to application-x-www-form-urlencoded. Obviously that Content-type is incorrect given that we’re no longer sending request data via an encoded url, but rather as form data.

I had left the incorrect urlencoded header value in by acccident, but after removing it, all values from the login form are now sent as expected:

XHR2 after Content-type request header fix.

XHR2 after Content-type request header fix.

Hope this saves someone some time.

This entry was posted in General. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *