0

I need to call a web service that requires a list of IDs in the form:

http://service_addr?itemID=34&itemID=36  ...

I tried setting up my service factory as:

.factory("doService", [$resource, function($resource) {
    return $resource("service_addr", {}, { 
        'create' : {method:POST, isArray:true} }); }])

In my controller I invoke the service with this code:

var ids = [];
angular.forEach(listofIDs, function(anId) {
    ids.push( { itemID : anID } );
}
doService.create(ids, {}, function (response) {
    ... do response stuff 
}

in the console the POST return a 40 Bad request error. The request is malformed in the parameters as shown below:

    http://service_addr?0=%5Bobject+Object%5D&1=%5Bobject+Object%5

How can I get the required parameters passed correctly?

JerryKur
  • 7,279
  • 6
  • 24
  • 33

2 Answers2

1

Adding to ricick's answer, you could also pass the IDs in the format

http://service_addr?itemIDs=34,36,38

by doing

ids.join(',')
ozandlb
  • 1,024
  • 2
  • 10
  • 23
  • Thanks. I assume this implies the developer of the service (which is not me) would have to alter their code? – JerryKur Oct 30 '13 at 01:43
  • They could grab the itemIDs attribute from the URL as in http://stackoverflow.com/questions/1403888/get-url-parameter-with-javascript-or-jquery and convert to an integer array as in http://stackoverflow.com/questions/4291447/convert-string-into-array-of-integers – ozandlb Oct 30 '13 at 03:55
  • Thanks for the info. I am having the developer switch to a POST and pass an array of IDs. Strangely, J2EE's parser does not care if we reuse the same name in the GET. I suspect it just creates name-value pairs without checking for unique names. – JerryKur Oct 30 '13 at 17:26
0

The issue is you can't have more than one parameter with the same name in GET, so even if angular could pass the data your server will only see one value (unless you're cheating and procssing the url string manually).

A better solution would be to something like:

http://service_addr?itemID0=34&itemID1=36itemID1=38&itemIDCount=3

that way you create a seperate parameter for each variable.

ricick
  • 5,694
  • 3
  • 25
  • 37