1

How to pass json object to WebApi as GET using $resource in angular?

My service:

pmsService.factory('Widgets', ['$resource', function ($resource) {

    var data = $resource('/api/:path/:id', {
        path: '@path'
    }, {
        getWidgets: {
            params: { path: 'widgets' },
            method: "GET",
            isArray: true
        },
        getWidget: {
            params: { path: 'widgets' },
            method: "GET",
            isArray: false
        },
        getWidgetData: {
            params: { path: 'widgets' },
            method: "POST"
        },
    });
    return data;

In angular controller:

Widgets.getWidgetData({ id: $scope.widget.id}, $scope.paramValues ).$promise.then(function (data) {
                $scope.widget.Data = data;
                $log.debug($scope.widget.Data);
            });

In Controller:

[Route("api/widgets/{id}")]
[HttpPost]
public Object Get(int id, dynamic prms)
   {
     ....
   }

This should sends 2 parameters to WebApi Controller, id and list of parameters for the Widget. Fiddler shows:

/api/widgets/31/%5Bobject%20Object%5D

So routing works correctly, but the object prms I received is empty.

Whistler
  • 1,897
  • 4
  • 29
  • 50

1 Answers1

1

I don't really understand what you're trying to do there but if you're trying to achieve a url parameter as in /api/widgets/31?foo=bar, then this is how I would do it.

angular
  .module('myMod', ['$resource'])

  .factory('Widgets',
  ['$resource', function ($resource) {
    return $resource(
      '/api/:path/:id/',
      {'path': '@path'},
      {
        getWidgets: {
          params: {path: 'widgets'},
          method: "GET",
          isArray: true
        },
        getWidget: {
          params: {path: 'widgets'},
          method: "GET",
          isArray: false
        },
        getWidgetData: {
          params: {path: 'widgets'},
          method: "GET",
          isArray: false
        }
      })
  }])

  .controller('WidgetsController',
  ['$scope', 'Widgets', function ($scope, Widgets) {
    $scope.widget = Widgets.getWidget({
      id: 1,
      foo: 'bar'
    });
  }]);

That would a make GET request to /api/widgets/1?foo=bar. You can include a nested object or an array like this and it will be serialised and appended to the url

  // In controller
  .controller('WidgetsController',
  ['$scope', 'Widgets', function ($scope, Widgets) {
    $scope.widget = Widgets.getWidget({
      id: 1,
      fields: ['name', 'price']
    });
  }]);

This would make a GET request to /api/widgets/1?fields=name&fields=price. I usually prefer to use the $httpParamSerializerJQLike serializer to serialize the parameters in this form /api/widgets/1?fields[]=name&fields[]=price which in my experience is less problematic. To use this serializer, you need to configure $http like so

angular
  .module('myMod')

  .config(['$httpProvider', function ($httpProvider) {
    $httpProvider.defaults.paramSerializer = '$httpParamSerializerJQLike';
  }])

Hope that helps

Ernest Okot
  • 880
  • 3
  • 8
  • 23
  • I want to send Json object which is the list of settings for the Widget. Your example sends string and I need to send object. I think @sirroco is right and I have to use POST, which I have done it and edit the question. Unfortunately this doesn't work – Whistler Nov 17 '15 at 15:42
  • the string can be decoded on the server side, am not sure how it's done with asp.net... take a look at this [answer](http://stackoverflow.com/a/1080721/4054434) to a similar question – Ernest Okot Nov 17 '15 at 15:52
  • I got an answer for the MVC, I'm porting app to WebApi and this bit doesn't work – Whistler Nov 17 '15 at 16:00
  • as an example `Widgets.getWidget({id: 1, settings: {height: 5, width: 6}})` would send a request like so `http://admin.etag.bea/api/widgets/1?settings={"height":5,"width":6}` – Ernest Okot Nov 17 '15 at 16:01
  • I know what you are saying, but when I stringy json object I get : /api/widgets/31/%7B%22LineID%22:%22SH5%22,%22MachineID%22:%227%22,%22DateFrom%22:%222015-10-25%22,%22DateTo%22:%222015-11-01%22%7D – Whistler Nov 17 '15 at 16:04
  • from [this](http://stackoverflow.com/a/21971987/4054434), looks like you can decode that 'stringy json object' to an object using `System.Web.Script.Serialization`... take a look – Ernest Okot Nov 17 '15 at 16:11
  • I think the issue is with double quote – Whistler Nov 17 '15 at 16:20