we are consuming a service that retrieve data from a GET request, filtering the data by the request body sent by data parameter on the $http method, it works great when is sent along with the params parameter, but it does not work when params is not set or is sent as an empty parameter , any idea of how resolve this?
Get requests can't have body
Could you post an example of code that you are doing?
sure.
--. when I sent the parameters, the call works fine.
var config = {
method: 'GET',
url: 'urlservice',
params: {paged:"true",pageSize:"1000",pageNumber:"1",orderBy:"ProjectNum"},
data: {projectStatus:["ON"]},
headers: {
'Authorization': 'Basic am1lamlhQGlhZGIub3JnOmExNjkwYmQ5ZWE0ZTRlMzlhODU1NWI5YzdmODFlZjIw',
'Content-Type': 'application/json; charset=utf-8'
}
}
$http(config).then(projectComplete, onError);
--. but, when call whitout parameters, the data is not sent
var config = {
method: 'GET',
url: 'urlservice',
data: {projectStatus:["ON"]},
headers: {
'Authorization': 'Basic am1lamlhQGlhZGIub3JnOmExNjkwYmQ5ZWE0ZTRlMzlhODU1NWI5YzdmODFlZjIw',
'Content-Type': 'application/json; charset=utf-8'
}
}
$http(config).then(projectComplete, onError);
I wonder if it is a browser things (rather than an Angular-thing).
@Sinapsis-Innovation , could you try with "raw" XMLHttpRequest
and see if the same thing happens ?
@gkalpak
this code works great an was sent without any params in the url string
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = callback;
function callback() {
if(xhr.readyState < 4){
return;
}
if(xhr.status !== 200){
console.log('problems');
return;
}
console.log(xhr.responseText);
}
xhr.open('GET', urlservice, true);
xhr.setRequestHeader('Authorization', 'Basic am1lamlhQGlhZGIub3JnOmExNjkwYmQ5ZWE0ZTRlMzlhODU1NWI5YzdmODFlZjIw');
xhr.send('{projectStatus:["ON"]}');
I can't reproduce the issue with v1.4.7
(see demo pen).
What version of Angular are you using, @Sinapsis-Innovation ?
Could you create a live reproduction of the problem (e.g. using CodePen, Plnkr etc) ?
@pkozlowski-opensource "Get requests can't have body"
In fact in the standard they can (at least nothing says they can't). But lot of servers and firewall implementation suppose they can't and drop them so using body in get request is a very bad idea.
@Sinapsis-Innovation Really, you shouldn't use body in your get request, you will have a lot of problems, not only with angular
In fact in the standard they can (at least nothing says they can't). But lot of servers and firewall implementation suppose they can't and drop them so using body in get request is a very bad idea.
Well, this is a gray area at best... Same as with DELETE.
But this is getting a bit academical as in practice sending body with GET request is a sure way of getting into funny pbs. I don't think angular should encourage this (and it would be kind of doing it if such option was added to the API)
I appreciate so much all your time you guys put on this, and your fast answers, it seems like the best solution it麓s change the service itself, so we will provide all this information to the person in charge of the web service to see how can he help us in this problem, but i麓m totally agree with you, that is not a good idea sending body in GET request
FWIW, Angular doesn't seem to be preventing you from sending GET requests with body (but you shouldn't do it regardless).
I am closing this, since there is nothing actionable here.
The XMLHttpRequest.send() method sends the request. If the request is asynchronous (which is the default), this method returns as soon as the request is sent. If the request is synchronous, this method doesn't return until the response has arrived. send() accepts an optional argument for the request body. If the request method is GET or HEAD, the argument is ignored and request body is set to null.
Most helpful comment
MDN