Rectangle 27 0

angularjs Angular, content type is not being sent with $http?


$http
data: ''

@josh-lee why do we need to provide data: '' ? its pretty strange to me. what about GET request ? any idea ?

God this is a life saver!!! Is this the default HTTP protocol behavior or Angular $http behavior?

You need to include a body with the request. Angular removes the content-type header otherwise.

but what if I want to send an object as data? I am working with an ASP.NET Generic HTTP handler, and for some reason, context.Request.Params["formData"] does not work, but a StackOverflow post led me to use string json = new StreamReader(context.Request.InputStream).ReadToEnd(); which works, but I am not exactly relieved to use it since I would rather access the submitted request parameter value by name.

Note
Rectangle 27 0

angularjs Angular, content type is not being sent with $http?


$http({
                method: 'GET',
                url:'/http://localhost:8080/example/test' + toto,
                data: '',
                headers: {
                    'Content-Type': 'application/json'
                }
            }).then(
                function(response) {
                    return response.data;
                }, 
                function(errResponse) {
                    console.error('Error !!');
                    return $q.reject(errResponse);
                }
Note
Rectangle 27 0

angularjs Angular, content type is not being sent with $http?


$http({
    url: 'http://localhost:8080/example/teste',
    dataType: 'json',
    method: 'POST',
    data: '',
    headers: {
        "Content-Type": "application/json"
    }

}).success(function(response){
    $scope.response = response;
}).error(function(error){
    $scope.error = error;
});

I am afraid it doesn't work in Angular 1.3.14, Content-Type is removed if there is no data. Empty data parameter solves the issue though...

Very Strange ! even for GET request also I had to provide the data: '' field. though we all know GET should not have a body ! any idea why angularjs is stripping off the content-type application/header when no data is provided ?

Note
Rectangle 27 0

angularjs Angular, content type is not being sent with $http?


// if header['Content-type'] is a POST then add data
'request': function (config) {
  if (
    angular.isDefined(config.headers['Content-Type']) 
    && !angular.isDefined(config.data)
  ) {
    config.data = '';
  }
  return config;
}
// set Content-Type for POST requests
angular.module('myApp').run(basicAuth);
function basicAuth($http) {
    $http.defaults.headers.post = {'Content-Type': 'application/x-www-form-urlencoded'};
}

Just to show an example of how to dynamically add the "Content-type" header to every POST request. In may case I'm passing POST params as query string, that is done using the transformRequest. In this case its value is application/x-www-form-urlencoded.

Then from the interceptor in the request method before return the config object

Note