Using FormData with $http

12,021 views
Skip to first unread message

kru...@seznam.cz

unread,
Aug 6, 2012, 7:38:02 AM8/6/12
to ang...@googlegroups.com
Anyone knows how to use FormData object with $http service? Because this doesn't seem to work:

var payload = new FormData();
payload.append("CustomField", "This is some data");
$http.post(url, payload);

Result from this is post with an empty object. Maybe set header to somethink else?

The reason I am using FormData is, that it can let you easily upload files.

Jack Chu

unread,
Aug 7, 2012, 1:40:21 PM8/7/12
to ang...@googlegroups.com, kru...@seznam.cz
I'm getting the same thing. The request payload is always empty even though I'm passing form data in.

soope...@gmail.com

unread,
Aug 7, 2012, 4:38:15 PM8/7/12
to ang...@googlegroups.com, kru...@seznam.cz
$http.post takes a 3rd parameter, a config object.  So try doing this:

$http.post(url, jQuery.param(payload), {
  headers:{
     'Content-Type':'application/x-www-form-urlencoded'
   }
}); 


This is not clear at all without doing a lot of reading or familiarity with class hierarchy.

kru...@seznam.cz

unread,
Aug 8, 2012, 4:14:54 AM8/8/12
to ang...@googlegroups.com, kru...@seznam.cz, soope...@gmail.com
I know about this, but you can't pass FormData object into jQuery param function and without param function it doesn't work either. I was thinking maybe a different header than urlencoded form? But I have no idea which one. What I am trying to do here is send FormData object, not just the usual data.

alex

unread,
Jan 13, 2013, 11:04:52 AM1/13/13
to ang...@googlegroups.com, kru...@seznam.cz, soope...@gmail.com
$HttpProvider's default request transform function looks like this:

// transform outgoing request data
transformRequest: [function(d) {
  return isObject(d) && !isFile(d) ? toJson(d) : d;
}],

and isFile(d) basically tests that d.toString() === '[object File]'. so, angular is trying to tranform your formdata with toJson() which won't work I guess.

You could make $http.post() work with formdata by overriding the tranform function, e.g.:

$http.post(url, myformdata, {
  transformRequest: function(data) { return data /* data here is myformdata */ }
})

alex

unread,
Jan 13, 2013, 12:55:19 PM1/13/13
to ang...@googlegroups.com, kru...@seznam.cz, soope...@gmail.com
more precisely, you'd do this (just tested it myself):

var payload = new FormData();
// populate payload
$http.post(postUrl, payload, {
  headers: { 'Content-Type': false },
  transformRequest: function(data) { return data; }
});

Default Content-Type is application/json if I'm not mistaken, and you can't set it manually to e.g. 'multipart/whatever' because if you're POSTing binary data (e.g. images) you'll need a boundary ID. Not setting Content-Type will make the underlying XHR browser implementation add a correct header, 'type; boundary'.

Shannon Deminick

unread,
May 23, 2013, 3:02:29 AM5/23/13
to ang...@googlegroups.com, kru...@seznam.cz, soope...@gmail.com
@Alex you are a legend! 

'Content-type': false

is what I've been hunting for all day... no luck using multipart/form-data since when I was validating the request on the back end it was always missing the boundary parameter... bit of a zany (hidden) way to force it to send the right headers but works! thx!

Jason Hostetter

unread,
Oct 21, 2013, 8:49:09 PM10/21/13
to ang...@googlegroups.com, kru...@seznam.cz, soope...@gmail.com
You were the last piece of the puzzle of at least 2 hours of googling. Thank you good sir.

Eduardo Martins Barbosa

unread,
Nov 26, 2013, 12:10:03 PM11/26/13
to ang...@googlegroups.com, kru...@seznam.cz, soope...@gmail.com
Great solution, Alex.

But it stopped working for me after I upgraded to Angular 1.2.2.

To fix it, just change Content-Type to undefined instead of false.

var payload = new FormData();
// populate payload
$http.post(postUrl, payload, {
  headers: { 'Content-Type': undefined },
  transformRequest: function(data) { return data; }
});


Milind Khandekar

unread,
Dec 31, 2013, 2:24:14 PM12/31/13
to ang...@googlegroups.com, kru...@seznam.cz, soope...@gmail.com
Eduardo, Alex, you guys rock!  A very, very happy new year to you guys.

Charlie Martin

unread,
May 18, 2014, 3:01:06 PM5/18/14
to ang...@googlegroups.com
I don't think I would have figured out to set the Content-type to undefined instead of false in a million years. Thank you!

Ravi G Teja

unread,
Nov 18, 2014, 9:34:04 AM11/18/14
to ang...@googlegroups.com
saved my day... :)
Reply all
Reply to author
Forward
0 new messages