Search code examples
angularjsjsoncontent-typerestangularrequest-headers

Restangular not able set default headers


I am trying to send a post request through Restangular. I want to pass json in content so I have added default headers in post request but still it gave 415 Unsupported media Type. When I checked the request headers in browser it still shows text/plain. This is my function

  this.create = function(data) {
      var deferred = $q.defer();
      var User = Restangular.all('user');

      User.post(data, {}, {'Content-Type': 'application/json'})
        .then(function(data) {
          $timeout(function() {
          deferred.resolve(data);
          });
        }, function(error) {
          deferred.reject(error);
        });
      return deferred.promise;
    }

The GET request works as expected. But I am unable to set content type in header of POST request?

I also tried to set this header globally thorugh

  .config(function($httpProvider, ServiceSettings, RestangularProvider) {

        console.log("urlddd " + ServiceSettings.apiUrl);

        RestangularProvider.setBaseUrl(ServiceSettings.apiUrl + '/api/');

        RestangularProvider.setRestangularFields({
            etag: 'Etag'
        });
        RestangularProvider.setDefaultHeaders({
            'Content-Type': 'application/json'
        });
        $httpProvider.interceptors.push('authInterceptor');
    });

But that also dint work. I've also tried this but no luck. Can anybody help?

Thanks


Solution

  • I've ended up creating a interceptor and setting each request with that content-type and adding below statement in my config.

    $httpProvider.interceptors.push('interceptor');