Andrew Louis
Andrew Louis

Reputation: 390

Post data with by $resource authentication?

I got $http request that works fine

var data = {name: 'Bite'}
var headers = { 
    'Content-type': 'application/json', 
    'Authorization':localStorage.getItem('jwt_token')} 

$http.post('/cats/api/', data, {headers: headers})
.then(
    function(thing) {
        console.log(thing);
    }, function(error) {
        console.log(error)
    });

And I want to use $resource for requests. I already made some simple requests with 'GET' method. But can't understand how to create request with 'POST' or 'PUT' data and authorization.

I tried many thongs like this:

var app = angular.module('Qwe', ['ngResource']);
app.factory('Catsfactory', ['$resource', '$window', 
    function($resource, $window) {
        return $resource('http://localhost:8000/cats/api/', {},
            {
                create: {
                    method: "POST",
                    cache: false,
                    isArray:false,
                    headers:{headers}  
                }      
            }
        )
}]);
app.controller('CatCtrl', ['$scope', '$http', '$window','$resource', 'Catsfactory',
    function($scope, $http, $window,$resource, Catsfactory) {
        var qwe = Catsfactory.create(data);
        qwe.$promise
        .then(function(thing) {
            console.log(thing);
        }, function(error) {
            console.log(error)
        });
}])

but that does not work out

Upvotes: 0

Views: 25

Answers (0)

Related Questions