0

I have a $resource called Livres I need to send to my API that wait a json typed like this :

{livres: {
  id: 1
  name: "bidule"
}}

This is the resource :

angular.module('TestApp').factory('Livres', [
  '$resource', function($resource) {
    return $resource('api/v1/livres/:id', {
      id: '@_id'
    }, {
      update: {
        method: 'PUT'
      }
    });
  }
]);

And the called method to save :

$scope.addLivres = function() { //create a new livres. Issues a POST to /api/livress
  $scope.livres.$save(function() {
    $state.go('adminLivres'); // on success go back to admin home i.e. adminLivres state.
  });
};

This only send a json like

  {id: 1
  name: "bidule"}

How can I make the json send to be encapsulated into a livres {} ?

Alain ANDRE
  • 305
  • 1
  • 13
  • don't you specify what goes into the resource though... so you could manipulate the input? Hang on, I don't think your factory is correct... check my answer to [this](http://stackoverflow.com/questions/30704283/angularjs-sending-options-instead-of-post/30704529#30704529) on forming factories.. – Callum Linington Jun 08 '15 at 11:32
  • Hi @CallumLinington, the factory is good. It works fine, but I have something to do with the **params** option [arguments](https://docs.angularjs.org/api/ngResource/service/$resource) I guess. I still can't figure how to do this. – Alain ANDRE Jun 11 '15 at 11:56
  • could you not do this: `Livres.$save({ livres: { id : 1, name: 'bidule' } })` – Callum Linington Jun 11 '15 at 12:12
  • I will give it a try, but I would appreciate not to have to specify all attributes :p – Alain ANDRE Jun 11 '15 at 16:38
  • No It does not work this way. This send the post request with get infos : `http://0.0.0.0:3000/api/v1/livres?livres={ id : 1, name: 'bidule' }` – Alain ANDRE Jun 12 '15 at 13:30
  • I modified my api to receive the json the angularjs basic way. I'm done. Thx @CallumLinington for help. – Alain ANDRE Jun 12 '15 at 13:43

0 Answers0