Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I post data as form data instead of a request payload?

In the code below, the AngularJS $http method calls the URL, and submits the xsrf object as a "Request Payload" (as described in the Chrome debugger network tab). The jQuery $.ajax method does the same call, but submits xsrf as "Form Data".

How can I make AngularJS submit xsrf as form data instead of a request payload?

var url = 'http://somewhere.com/'; var xsrf = {fkey: 'xsrf key'};  $http({     method: 'POST',     url: url,     data: xsrf }).success(function () {});  $.ajax({     type: 'POST',     url: url,     data: xsrf,     dataType: 'json',     success: function() {} }); 
like image 646
mjibson Avatar asked Jul 11 '12 22:07

mjibson


People also ask

What is the difference between form data and request payload?

"Form Data" is a subset of Request Payload in which the body is encoded as key1=value1&key2=value2. Whenever Google Chrome can distinguish Form Data from a generic Request Payload, it customizes the formatting.

How is form data sent in post request?

Short answer: in POST requests, values are sent in the "body" of the request. With web-forms they are most likely sent with a media type of application/x-www-form-urlencoded or multipart/form-data .

Is request payload same as request body?

So Yes, they are the same thing. Show activity on this post. So basically the only difference between HTTP message body and HTTP message payload body is encoding (but only if present). So generalizing the term request payload = request body.

What is request payload in POST method?

A request payload is data that clients send to the server in the body of an HTTP POST, PUT, or PATCH message that contains important information about the request.


2 Answers

The following line needs to be added to the $http object that is passed:

headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'} 

And the data passed should be converted to a URL-encoded string:

> $.param({fkey: "key"}) 'fkey=key' 

So you have something like:

$http({     method: 'POST',     url: url,     data: $.param({fkey: "key"}),     headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'} }) 

From: https://groups.google.com/forum/#!msg/angular/5nAedJ1LyO0/4Vj_72EZcDsJ

UPDATE

To use new services added with AngularJS V1.4, see

  • URL-encoding variables using only AngularJS services
like image 63
mjibson Avatar answered Oct 12 '22 05:10

mjibson


If you do not want to use jQuery in the solution you could try this. Solution nabbed from here https://stackoverflow.com/a/1714899/1784301

$http({     method: 'POST',     url: url,     headers: {'Content-Type': 'application/x-www-form-urlencoded'},     transformRequest: function(obj) {         var str = [];         for(var p in obj)         str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));         return str.join("&");     },     data: xsrf }).success(function () {}); 
like image 45
Anthony Avatar answered Oct 12 '22 06:10

Anthony