3

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?

ssilas777
  • 9,672
  • 4
  • 45
  • 68
Pooja
  • 107
  • 1
  • 8
  • @StaffordWilliams Added the error in my question – Pooja Sep 02 '15 at 12:57
  • you can simple press F12 and see what requests are send from your code (ex. in 'network' in chrome), so you can see the difference between them – rzysia Sep 02 '15 at 13:02
  • `$http.put` is just a wrapper. Take a look at the source, line 1153 and 1183-1193 https://github.com/angular/angular.js/blob/master/src/ng/http.js – Yoni Levy Sep 02 '15 at 13:10

1 Answers1

5

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 :)

Community
  • 1
  • 1
Pooja
  • 107
  • 1
  • 8