2

I'm developing a jsp which sends POST requests to a servlet. I would like to write content in raw payload instead of key-value form.

var addItemApp = angular.module('addItem',[]);
addItemApp.controller('addItemCtrl', function($scope, $http){
    $scope.addItemBnClicked = function(){
        var rqst = $http.post('http://127.0.0.1:8180/JSP/ws', "This is a test content");

        rqst.success(function(rspnData, status, headers, config){
            alert("Status: " + status);
        });

    }
});

Checking on the server side, I found that the payload doesn't contain anything. Please help, Thanks. ^^

%%%%%%%%%%%%%%% Edit %%%%%%%%%%%%%%%

I use the following code to get the payload.

String line = "";
String rqstRawBody = "";
while((line = servletRqst.getReader().readLine()) != null){
    rqstRawBody += line;
}

System.out.println("rqstRawBody: " + rqstRawBody);

The rqstRawBody is finally an empty string. I believe the above java code is okay, as I get raw payload correctly for those requests sent using the chrome-app Rest Client.

1
  • the function is ok which type of data you are recieving in server side Commented Jul 29, 2015 at 10:28

1 Answer 1

2

You should change the Content-Type HTTP header. By default Angular sets that to application/json for a POST request.

var config = { headers: { 'Content-Type': 'text/plain' } };
var data = { someKey: 'SomeValue'};
$http.post('/url', data, config).then(...);

Or you can set it as the default for all requests as shown in the documentation:

module.run(function($http) {
  $http.defaults.headers.common['Content-Type']= 'text/plain'
});
Sign up to request clarification or add additional context in comments.

1 Comment

@chunchiuChan look at edit, it was missing with data option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.