0

I can't find a solution to send a PUT request data as Form Data in AngularJs.

Here's my code :

services.factory('appsInfoFactory', ['$http', function ($http) {
    var apps = {};
    var callPut = function (url, data, callback) {
      var headers = {'Content-Type': 'application/x-www-form-urlencoded'},
      User = 'aaaa',
      Secret = 'dummysecret',
      param = 'User=' + User + "&Secret=" + Secret;
      $http.put(url + "?" + param, data, headers).success(callback);
    };
    apps.registerApp = function (appName, data, callback) {
      callPut("/apps/" + appName, data, callback);
    };
    return apps;
}]);

$scope.addApps = function (Name, Repo, Root, Email, Internal, NonAtlantis) {
    var User = 'aaaa',
    Secret = 'dummysecret',
    data = JSON.stringify({Name, Repo, Root, Email, Internal, NonAtlantis, User, Secret});
    console.log(data);

    appsInfoFactory.registerApp(name, data, function (val) {
      console.log(val);
    });
}

But it sends data as Request Payload and I want to send it as Form Data as per my backend code's requirement.

Thanks for any help.

  • Your answer is here: http://stackoverflow.com/a/11443066/2528925 – David Boskovic Oct 20 '15 at 07:08
  • Possible duplicate of [How can I post data as form data instead of a request payload?](http://stackoverflow.com/questions/11442632/how-can-i-post-data-as-form-data-instead-of-a-request-payload) – David Boskovic Oct 20 '15 at 07:09

1 Answers1

0

Take a look at last line in the function callPut. It should be like this :-

$http.put(url + "?" + param, data, {'headers': headers}).success(callback);

Last param to $http.PUT method is a config object in which it will be expecting a key as "headers" so try passing it as an object.

and I will suggest to make var config = {'headers':headers} and then pass this config object to PUT.

$http.put(url + "?" + param, data, config).success(callback);

Pranav
  • 2,054
  • 4
  • 27
  • 34