Prometheus
Prometheus

Reputation: 33625

Angular interceptor gives blank output with no error

I want to inject Content-Type in my $http. request. I have the following interceptor only I don't get any errors/output, what I'm I doing wrong?

  .factory('Interceptor', ['$injector', '$q', function ($injector, $q) {
        return function () {
            return {
                request: function (config) {
                    var deferred = $q.defer();
                    config.headers['Content-Type'] = 'application/x-www-form-urlencoded';

                    deferred.resolve(config);
                    return deferred.promise;
                }
            };
        };

    }])

    .config(['$httpProvider', function ($httpProvider) {
        $httpProvider.responseInterceptors.push('Interceptor');
    }]);

Upvotes: 0

Views: 45

Answers (1)

Vikk
Vikk

Reputation: 647

.factory('Interceptor', ['$injector', '$q', function ($injector, $q) {
    return {
        request: function (config) {
            config.headers = config.headers || {};
            config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
            return config;
        },
        response: function(response) {
            return response || $q.when(response);
        }
    };
}])

I wish I had more time to test this out, but give it a try. It's a variation of code I used before to add a 'Bearer' token to the headers.

Upvotes: 1

Related Questions