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.

I would like to make a request to a REST-service in which the query parameters contain an array of strings:

productRestService.getProductsInfo(productIdsArray,"id,name,rating").$promise.
               then(function(productData) { // success: Produktdaten auslesen                
                    updateProductList(productData);

                }, function (error) {
                    console.log("Status: " + error.status);       
                });

The Resource-Service is as follows:

productRestService.getProductsInfo = function(productIds, properties) {
        console.log('productRestService.getProductsInfo(): productIds' + productIds);
        var productInfoResourceData;
        var ProductInfoResource = $resource('/rest/products/productsInfo/:productIds/:properties',
            {
                productIds:'@productIds',
                properties:'@properties'
            }
        );
        productInfoResourceData = ProductInfoResource.query(
            {
                productIds: productIds,
                properties: properties
            }
        );
        return productInfoResourceData;

    }

Calling the service results to an 404-Error, because the default behaviour of the $resource object is that it expects an array of an object when "query" is used.

How can I achieve that my $resoure-service will accept an array of strings? I tried to use "transformRequest" (see snippet below), but that did not work either.

  {
                query: {
                  method: 'GET',
                  isArray: true,
                  transformResponse: function (data, headers) {
                    var tranformed = [];
                    [].forEach.call(eval(data), function (d) {
                        tranformed.push({ name: d });
                    });
                    return tranformed;
                    }
                }
            }

A console.log within the function of the REST service productService.getProductsInfo shows the correct data that the service received:

["212999cc-063b-4ae8-99b5-61a0af39040d","17e42a28-b945-4d5f-bab1-719b3a897fd0","9307df3e-6e7a-4bed-9fec-a9d925ea7dc0"]

The URL is correct with the other REST-URLS and should look this way (and is being concatenated to the domain accordingly):

'/rest/products/productsInfo/:productIds/:properties'

EDIT: The other functions within the productService responds in order, they do not use arrays but JSON objects and do not show unexpected behaviour.

share|improve this question
    
Confused; I imagine the 404 would result from an invalid request URL being constructed -- nothing to do with the response (I imagine transformResponse is never even called). Can you provide the URL that is being constructed and the way you want the URL to look? –  Daniel Feb 18 at 17:19
    
I edited my question with comments and the URL you requested. Changing the type of parameter 'productIdsArray' within the requesting command from array to JSON (with JSON.parse) resolves the 404 Error but I do not quest any data. –  Pille Feb 18 at 17:35
    
You provided the URL template, not the URL. Like @Daniel mentioned, you should also provide a manually constructed URL to show how you want the URL (not the template) to look and, more importantly, how the server expects it to look. –  billisphere Feb 18 at 18:19

1 Answer 1

up vote 1 down vote accepted

(This was originally a comment, but it needed cleanly formatted code samples.)

I suspect your :productIds template parameter is getting filled into the template as "[object Object]". I've only seen your template URL, not the actual constructed URL, so I can't be sure.

If your server is expecting a URL where the :productsIds template parameter is JSON, like for example ---

rest/products/productsInfo/["id1","id2","id3"]/{"prop1":true,"prop2":false}

--- then try editing your getProductsInfo definition to something like this:

productRestService.getProductsInfo = function (productIds, properties) {
    var ProductsInfo = $resource('/rest/products/productsInfo/:productIds/:properties', {
        productIds: function () {
            return angular.toJson(productIds);
        },
        properties: function () {
            return angular.toJson(properties);
        }
    });
    return ProductsInfo.query();
}

(Fair warning, I didn't test this code. It's just a quick edit of your example.)

This way, you're making sure that the parameter values are converting to the JSON that the server expects (if the server is expecting JSON in the URL, that is).

share|improve this answer
    
Thank you for your answer and your help! How can I get the actual constructed URL of the REST call? –  Pille Feb 18 at 18:31
    
Your solution brings more output, but the data delivered are a lot of data: [object Object],[object Object],[object Object],true Those objects should be an array instead of arrays of objects. How do I convert those? "angular.toArray" does not seem to work. –  Pille Feb 18 at 18:39
    
@Pille, unfortunately I'm not sure about getting the constructed URL. When I saw your question I spent a few minutes looking into it, but I didn't find anything. About the new output, if you're no longer getting 404'd by the server and are now receiving a response with data, this particular question might be resolved and you might want to ask a new question. Make sure to include enough details (request logic, detailed description/format of data retrieved vs. data desired, etc.) to make it easier to help you. –  billisphere Feb 18 at 18:52
1  
I learnt that with CHROME it is possible to get the constructed URL with inspecting the "network" status. However you are right about the type of question as it had changed now. I do not get an 404 error any more and will proceed as you suggested. Again, thank you very much. –  Pille Feb 19 at 19:03
    
@Pille, ah neat, I'm a bit embarrassed I didn't realize the Network tab in DevTools would have that :-) Good to know. Thank you as well. –  billisphere Feb 19 at 19:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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