raonirenosto raonirenosto - 1 year ago 184
AngularJS Question

Angular, content type is not being sent with $http

Angular is not adding the correct content type option, I tried the following command:

url: "http://localhost:8080/example/teste",
dataType: "json",
method: "POST",
headers: {
"Content-Type": "application/json"
$scope.response = response;
$scope.error = error;

The code above generates the following http request:

POST http://localhost:8080/example/teste HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 0
Cache-Control: no-cache
Pragma: no-cache
Origin: http://localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
Content-Type: application/xml
Accept: application/json, text/plain, */*
X-Requested-With: XMLHttpRequest
Referer: http://localhost:8080/example/index
Accept-Encoding: gzip,deflate,sdch
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: JSESSIONID=C404CE2DA653136971DD1A3C3EB3725B

As you can see, instead of "application/json", the content type is "application/xml". Am I missing something here ?

Answer Source

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

Add data: '' to the argument to $http.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download