0

My server REST Jsersey receive Json and query param. I have 3 query params: username, password and email

@POST
    @Consumes({MediaType.APPLICATION_JSON})
    @Path("register")
    public Response register(@QueryParam("email") String email, @QueryParam("username") String userName, @QueryParam("password") String password) {
        System.out.println("username: " + userName);
        System.out.println("password: " + password);
        System.out.println("email: " + email);

        if (TextUtil.isEmpty(userName) || TextUtil.isEmpty(password) || TextUtil.isEmpty(email)) {
            return Response.status(Response.Status.BAD_REQUEST)
                    .header("Access-Control-Allow-Origin", "*")
                    .entity(ErrorUtil.badRequest("Field should not be empty")).build();
        }

        List<User> listUser = findAll();
        for(User user: listUser) {
            if (user.getUsername().equals(userName)) {
                return Response.status(Response.Status.BAD_REQUEST)
                        .header("Access-Control-Allow-Origin", "*")
                        .entity(ErrorUtil.badRequest("This username is already registered"))
                        .build();
            }
            if (user.getEmail().equals(email)){
                return Response.status(Response.Status.BAD_REQUEST)
                        .header("Access-Control-Allow-Origin", "*")
                        .entity(ErrorUtil.badRequest("This email is already registered"))
                        .build();
            }
        }

I try to user Jquery to send a request to the server but I got 415 Unsupported media type

var signUp = function () {
    $("#formSignUp").submit(function (e) {
      // var userName = $('#textUserNameSignUp').val();
      // var email = $('#textEmailSignUp').val();
      // var password = $('#textPasswordSignUp').val();

        e.preventDefault();

        $.post('http://localhost:43319/BR/webresources/users/register',
        { username: "hello", password : "123", email : "email"},
            function(returnedData){
              console.log(returnedData);
        })
        .fail(function(){
            console.log("error");
        });
    });
}

Can you please show me what's wrong and suggest me how to do it right

tereško
  • 58,060
  • 25
  • 98
  • 150
coinhndp
  • 2,281
  • 9
  • 33
  • 64

1 Answers1

0

You need to specify the contentType for the data you are sending

try adding

$.ajaxSetup({
  contentType: "application/json; charset=utf-8"
});

before you signUp function

Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
  • It works and now I deal with bad request becasue I receive username, password and email that are null. It means that I fail to send the query param –  coinhndp Nov 26 '17 at 12:01
  • I log the text receive in server and I got username=hello&password=123&email=email .How can I parse it to json –  coinhndp Nov 26 '17 at 12:04
  • see here it might help you out why it is query string [link1](https://stackoverflow.com/questions/5876809/do-http-post-methods-send-data-as-a-querystring) [link2](https://stackoverflow.com/questions/10406588/api-requires-post-arguments-in-a-query-string) – Muhammad Omer Aslam Nov 26 '17 at 12:13