0

I am executing a POST request with Angular JS against a Java servlet running on Google App Engine. The code works fine, unfortunately I've noticed that if one (or more) parameters are very long I get the following error:

Error: Length Required

POST requests require a Content-length header.

This is my Angular JS code:

app.controller('mycontroller', ['$scope', '$http', '$log', function($scope,$http,$log) {

this.myfunction = function() {

$http({
    method: 'POST',
    url: '/myservlet',
    params: { method: 'my_method', param1 : $("#param1").val(), param 2: ... }
}).success(function(data) {
                
}).error(function(data) {
                
});

};
}

Param1, param2 etc are different fields, mostly textarea though. Any suggestion? Honestly I don't know what to do.

EDIT: I tried setting the Content-Length in the header myself but I get an error as I expected...

headers : { 'Content-Length' : 0 },

Refused to set unsafe header "Content-Length"

So basically, if I set the Content-Length the browser refuses to execute the call, if I don't set it then the server says it's required... What can I do?

Community
  • 1
  • 1
raz3r
  • 3,071
  • 8
  • 44
  • 66

1 Answers1

1

The params argument you are using is used to pass GET variables as part of the url even when method is POST ($http docs). To pass information in the body of the POST request you have to use the data argument.

So, your request should be:

$http({
    method: 'POST',
    url: '/myservlet',
    data: { method: 'my_method', param1 : $("#param1").val(), param 2: ... }
}).success(function(data) {

}).error(function(data) {

});
MeLight
  • 5,454
  • 4
  • 43
  • 67
  • That way on servlet side request.getParameter("method") returns null :( – raz3r Jun 03 '15 at 09:16
  • Just a sec, gonna look at how java eats this – MeLight Jun 03 '15 at 09:18
  • http://stackoverflow.com/questions/14525982/getting-request-payload-from-post-request-in-java-servlet they say here that you need to use `getReader()` to read the body of the request – MeLight Jun 03 '15 at 09:23
  • I dont want to change my existing code server side, is there any way to set the content-length and/or keep using request,getParameter? Thank you. – raz3r Jun 03 '15 at 12:25
  • Not that I'm familiar with. Sorry – MeLight Jun 03 '15 at 14:14