0

I honestly thought I knew how to do this, apparently not.

Here is my basic resource for creating a user.

@POST
@Path("create")
@Timed
@UnitOfWork
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public long createUser(@PathParam("username") String username,
                         @PathParam("password") String password)
{
    User userToCreate = new User();
    System.out.println("**********************************************");
    System.out.println(username + " " + password);
    System.out.println("**********************************************");

    userToCreate.setUsername(username);
    userToCreate.setPassword(password);

    // Save to database.
    return userDAO.create(userToCreate);
}

Very simple, the System.out lines are just to help me debug, they will be removed when this works (and yes, I will add encryption, too!)

Anyways, it turns out, that no matter what I seem to do - when sending data to this via PostMan, the value for username and password are ALWAYS null... I have no idea what the hell is going on.

I send the fields "username" and "password" as raw json withid the body of the request.

Am I missing something?

MickeyThreeSheds
  • 986
  • 4
  • 23
  • 42
  • Yeah, I should have mentioned that, sorry - I do have that set - but for some reason, everything is null... – MickeyThreeSheds Sep 11 '17 at 01:06
  • Sorry, realized the issue and deleted my comment hoping you wouldn't see it :). You are using PathParam but sending the data in the body (not the path) – Pace Sep 11 '17 at 01:07

2 Answers2

1

You have to send those parameters in the query string, not in the body. For instance:

POST http://localhost:8080/create?username=jsmith&password=foobar

Another option:

If you really want to send it in the body, create an object to hold the data.

public class UserPass {
    private String username;
    private String password;

    public void setUsername(String username) {
         this.username = username;
    }

    public String getUsername() {
         return username;
    }

    public void setPassword(String password) {
         this.password = password;
    }

    public String getPassword() {
         return password;
    }
}

And change your method signature to look like this:

public long createUser(UserPass userPass) {
   User userToCreate = new User();
   System.out.println("**********************************************");
   System.out.println(userPass.getUsername() + " " + userPass.getPassword());
   System.out.println("**********************************************");

   userToCreate.setUsername(userPass.getUsername());
   userToCreate.setPassword(userPass.getPassword());

   // Save to database.
   return userDAO.create(userToCreate);    
}

Then post with JSON something like this:

{
   "username": "jsmith",
   "password": "foobar"
}

Be sure Content-Type is set to "application/json" on the POST request.

tom
  • 1,331
  • 1
  • 15
  • 28
  • Technically one would use `QueryParam` if they want to put them in the query like that. `PathParam` is for parameters passed in as the path part of the URI (e.g. /users/{username}/{password} – Pace Sep 11 '17 at 01:28
  • Yes you're right about that. However as you said you will have to encrypt your username and password. It is never a good idea to send that kind of payload as part of the query string (even if encrypted) unless you are using HTTPS. It will show up in access logs and such. See https://stackoverflow.com/questions/323200/is-an-https-query-string-secure – tom Sep 11 '17 at 02:38
1

You are using @PathParam annotation to retrieve the inputs for your resource function but sending the data using the Body.

You need to either send the username and password as path parameters like

xyz.com/create/{username}/{password}

or

You need to create a request class and use it as input parameter to get the data from body of your request like

@Getter
@Setter
public class RegisterRequest{
     private String username;
     private String password;
}

And use it like

@YourAnnotations
public long create(RegisterRequest request){
     //Your code for creating user.
}
Sahil Chhabra
  • 10,621
  • 4
  • 63
  • 62