3

I am trying to post query parameters from angular JS to rest server. But the value received in the rest is always null. Can anyone please tell what i am missing?

Angular code,

$http({
                method : 'POST',
                url    : '/rest/user/changePassword',
                data   : {'id':$scope.user.id, 'password':$scope.user.password, 'newpassword':$scope.user.newpassword},
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }).success(function(data){
                console.log(' data ');
            });

Rest code,

    @Path("/changePassword")
        @POST
        @Produces(MediaType.APPLICATION_JSON)
        @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
        public Response changePassword(@QueryParam("id") int id,
                @QueryParam("password") String password,
                @QueryParam("newpassword") String newpassword, @Context UriInfo request) {


            System.out.println(" id " + id + " re " + request.getQueryParameters().getFirst("id"));
            System.out.println(" password " + password  +   " "+  request.getQueryParameters().getFirst("password"));

            System.out.println(" newpassword " + newpassword + " " + request.getQueryParameters().getFirst("newpassword")  );
return new Response('asd');    
        }

I tried $.param in angular side, tried to get parameters from HttpServletRequest but the result is always null.

2 Answers 2

1

@QueryParam is used to pick up values passed across in a query string in Get Requests.

Create a Java Object that has the variables matching your data structure, and use that as an input instead of the separate variables.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply, is there any other way to get this parameters instead of creating java object.
If i use get method then is it possible to retrieve the query param?
You could, but, based on what your service is called, I would not recommend it. Your attributes are not really query parameters, there are data items to be committed to the database, so a Post is correct.
1

Since you are using HTTP POST, you need to replace @QueryParam with @FormParam. Your code would then be:

@Path("/changePassword")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response changePassword(@FormParam("id") int id,
                               @FormParam("password") String password,
                               @FormParam("newpassword") String newpassword, @Context UriInfo request) {


    System.out.println(" id " + id + " re " + request.getQueryParameters().getFirst("id"));
    System.out.println(" password " + password  +   " "+  request.getQueryParameters().getFirst("password"));

    System.out.println(" newpassword " + newpassword + " " + request.getQueryParameters().getFirst("newpassword")  );
    return new Response('asd');
}

Check out this tutorial

You also need to force AngularJS to POST the data as form parameters like so:

$http({
    method : 'POST',
    url : '/rest/user/changePassword',
    data : $.param({
        'id' : $scope.user.id,
        'password' : $scope.user.password,
        'newpassword' : $scope.user.newpassword
    }),
    headers : {
        'Content-Type' : 'application/x-www-form-urlencoded'
    }
}).success(function (data) {
    console.log(' data ');
});

Check out this SO answer.

The way your code used the $http object forces angular to post a json object

2 Comments

Can you post the HTTP request that is sent over the network? It is easy to see this using the network tab on Firebug or Chrome dev tools
Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8,ta;q=0.6 Cookie: JSESSIONID=C5AADCF13827B76669877E9645E8A3E4 Form Dataview sourceview URL encoded {"id":1,"password":"D A2ph3tWAEE92N1Lh8J7dBtBlg:","newpassword":"asd"} Response Headersview source Allow:GET,OPTIONS Content-Length:1034 Content-Type:text/html;charset=utf-8 Date:Tue, 24 Jun 2014 11:02:50 GMT Server:Apache-Coyote/1.1 ConsoleSearchEmulationRendering

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.