A.khalifa
A.khalifa

Reputation: 2496

POST jersey with HeaderParam annotation

I get null when I send a POST. Please someone know how to pass value with Angular using @HeaderParam in Jersey?

I have this in backend:

 @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Users loginUser(@HeaderParam("username")String username,@HeaderParam("password") String password){
        System.out.println("What happen"+userService.loginService(username, password));
       return userService.loginService(username, password);
    }

and this in front-end, using Angular:

 $scope.getUserFunction = function () {
      $http({
        method  : 'POST',
        url     : 'http://localhost:8080/JersyBackEnd/resources/login'+data,
        contentType:'application/json',
        data    : {username: "akram",password:"yes"},
      })
        .then(function(data) {
          console.log(data);
        });
    };

I get 204 because of the null data.
Can anyone help me, either using HeaderParam, PathParam or FormParm annotations in Jersey?

Upvotes: 0

Views: 1679

Answers (2)

hari
hari

Reputation: 144

When you use @HeaderParam, you have to pass the values from .JS using headers: { 'something': 'anything' }. As i can see currently you are using data : {username: "akram",password:"yes"}. try to use headers: { 'something': 'anything' } instead of this.

Please try header, hope it will solve your null issue.

Upvotes: 0

gustavohenke
gustavohenke

Reputation: 41440

data is sent as the request body. What you really want is headers.
From the docs:

  • data{string|Object} – Data to be sent as the request message data.
  • headers{Object} – Map of strings or functions which return strings representing HTTP headers to send to the server. If the return value of a function is null, the header will not be sent. Functions accept a config object as an argument.

Example:

$http({
  ...
  headers: {
    username: "akram",
    password: "yes"
  }
})

Upvotes: 1

Related Questions