Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I am a novice to AngularJs and REST service and want to know how can I pass an array of object to REST-POST call in angular js using $resource. I know how to pass a single parameter but not sure how to pass an array. This is my code for Get call where I am passing a single parameter. Can anyone tell me , how can I achieve the same thing with POST + array. Thanks!!

 var services = angular.module('myApp.services', ['ngResource']);
        services.factory('AngularIssues',
            function($resource){
            return $resource('http://localhost:8181/MyRESTService/services/UserInfo/:id', {} ,{
                get:{method:'GET' , params: {id: '@id'} }
             } );
        });
share|improve this question

marked as duplicate by Robert Harvey Feb 3 '14 at 21:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Look here: stackoverflow.com/questions/12002560/… –  tenthfloor Jan 28 '14 at 0:32
    
@tenthfloor - I tried the solution posted in your code but I am getting this error "No message body reader has been found for request class List, ContentType : application/octet-stream". Basically my REST service is expecting a list. –  user911 Jan 28 '14 at 17:09

1 Answer 1

up vote 1 down vote accepted

In your controller you'll need to do something like this. Where you are passing your POST request as a parameter of the save() function. Working jsFiddle. You can verify this using Chrome's Dev Tools under Networks.

Have a look at the AngularJS $resource documentation, there are some examples of how to make a POST call to the API.

services.factory('AngularIssues',
function($resource){
   return $resource('/echo/json/', {} ,{
       get:{method:'GET' , params: {id: '@id'} }
   } );
});

services.controller('AppController', ['$scope', 'AngularIssues', 
function($scope, AngularIssues) {

    AngularIssues.save({ "theArray": [{ name: "Object 1" }, { name: "Object 2" }] })
    .$promise.then(function(res) {
       $scope.done = "I AM DONE!";
    });

}]);
share|improve this answer
    
@J.P.Armstrong-> I tried this code and getting error "No message body reader has been found for request class List, ContentType : application/octet-stream" $scope.data1= [{employeeId:"12345", firstName: "Neon", lastName: "Milan",designation:"Business Analyst" , department:"Dep",salary:"*******",rating:"1" , joiningDate:"11/2/2007" , employeeType:"permanent" , manager:"aaaa" }]; UpdatePopulation.saveData({}, $scope.data1); My REST service expects a list. –  user911 Jan 28 '14 at 17:39
    
Update your question with the code, where are you defining: UpdatePopulation.saveData({}, $scope.data1); –  J.P. Armstrong Jan 28 '14 at 18:47
    
Thanks J.P.Armstrong and @tenthfloor!! Both of your answers have helped me and I am finally able to send array in request body to REST service using $resource. –  user911 Jan 28 '14 at 19:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.