Search code examples
angularjshttpangularjs-httphttp-put

Difference between $http.put and $http({method:'PUT'}) in AngularJS


I have to 'PUT' some json data to a server. The below code is throwing an error

$rootScope.request.data = {"name": "John", "surname":"Doe"}
var uri = //some REST API
var action = $http({
    method: 'PUT',
    url: uri,
    data: $rootScope.request.data
});

The error thrown is:

Flow not found for resource: Resource{displayName='null', uri='/signup'} (org.mule.module.apikit.exception.ApikitRuntimeException). Message payload is of type: NullPayload

But when I do this, it works

$rootScope.request.data = {"name": "John", "surname":"Doe"}
var uri = //some REST API
var action = $http.put(uri, $rootScope.request.data);

The 'action' is then pushed in an array and the requested are fired using a $q.all. The success and error is handled in the $q

Was wondering what is the difference between them? Have I missed something in my first request?


Solution

  • Solved the issue. Thanks to @rzysia for the pointer.

    When I compared the requests, the Content-Type in the 1st case was sent as 'text/plain' and in the second case it was 'application/json'. The REST API needed 'application/json' as the content type.

    Adding the below code did the trick

    $rootScope.request.data = {"name": "John", "surname":"Doe"}
    var uri = //some REST API
    var action = $http({
        method: 'PUT',
        url: uri,
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        data: $rootScope.request.data
    });
    

    These links were helpful: https://github.com/angular/angular.js/issues/2149 and Content-Type header not being set with Angular $http

    And big thanks to all who put in their 2 bit :)