0

I am working with a task based project, where I have a RESTful background with stores JSON data about the tasks. Using Flask in backend, and AngularJS in the front.

This lists all tasks for me. How do I go about converting this to a POST call where I send a specific task id to retrieve all details about that task?

2 Answers2

1

If you have restangular list it means each instance inside has restagular helper functions. For you:

   const list =Restangular.all('tasks').getList().then(function(result) {
        $scope.tasks = result;
    });
    ....
    $scope.tasks[0].post()

But notece, that restangular takes id as post parametr. i.e {id: 2} will be /url/2

Errorpro
  • 2,253
  • 2
  • 16
  • 17
  • And? You do transfor your request. It doesn't matter. When you use myarr[0] you get restangulized object which have all of this methods: https://github.com/mgonto/restangular (post, get, save, put ...) – Errorpro Nov 06 '15 at 05:51
  • In your controller: `function doPost = function(task) { task.post() }` in your html `
    – Errorpro Nov 06 '15 at 06:12
  • No, if you want to do post you need define it: 'restangular.one('task', id).get() or post()` – Errorpro Nov 06 '15 at 06:27
0

How do I go about converting this to a POST call where I send a specific task id to retrieve all details about that task?

Actually, if you would like to retrieve specific task with all its details, GET request would be more suitable than POST.

In RESTful application GET is supposed to just read elements (with no user-modifications). POST/PUT is reserved for create or update the element, read relevant topic: PUT vs POST in REST

So in your case, this will get all your tasks, and assign them to $scope.tasks model:

Restangular.all('tasks').getList().then(function(response) {
    $scope.tasks= result;
});

Above getList() method will make GET request at URL: http://yourdomain.com/tasks

You can also use different notation to achieve the same (check Restangular documentation for details):

$scope.tasks = Restangular.all('tasks').getList().$object;

or you can assign Restangular object to the Task variable, which is just a container, holding all valuable Restangular methods and routing. Then you can call various methods on that object:

var Tasks = Restangular.all('tasks');
$scope.tasks = Tasks.getList().$object;

After that you can access specific task in the tasks collection, and then call some other methods on it:

var MyTask = $scope.tasks[0];
MyTask.get() // this will do GET at URL http://yourdomain.com/tasks/123, assuming 123 is id of existing task
MyTask.post() // this will do POST at above URL
MyTask.put() // this will do POST at above URL
MyTask.save() // this will do PUT or POST - depends if MyTask is going to be udpated or created.

You can also query for specific task manually at URL http://yourdomain.com/tasks/123:

Tasks.one('tasks', 123).get().then(function(response) {
    $scope.mytask = response
});
Community
  • 1
  • 1
rsobon
  • 1,012
  • 2
  • 12
  • 26