1

If I have something like:

@PUT
@Path("/login")
@Produces({"application/json", "text/plain"})
@Consumes("application/json")
public String login(@FormParam("login") String login, @FormParam("password") String password) throws Exception
{
    String response = null;
    response = new UserManager().login(login, password);
    return response;
}

How can I enter the both parameters to Test my REST services (in the Content field)? Isn't something like:

{"login":"xxxxx","password":"xxxxx"}

Thanks

user2144555
  • 1,315
  • 11
  • 34
  • 55

1 Answers1

1

Form parameter data will only be present when you submit ... form data. Change the @Consumes type for your resource to multipart/form-data.

@PUT
@Path("/login")
@Produces({ "application/json", "text/plain" })
@Consumes("multipart/form-data")
public String login(@FormParam("login") String login,
        @FormParam("password") String password) {
    String response = null;
    response = new UserManager().login(login, password);
    return response;
}

Then on your client side, set:

  • Content-Type: multipart/form-data
  • Add form variables for login and password

On a side note, assuming this isn't for learning, you will want to secure your login endpoint with SSL, and hash the password before sending it across the wire.


EDIT

Based on your comment, I am including an example of sending a client request with the required form data:

try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost post = new HttpPost(BASE_URI + "/services/users/login");

    // Setup form data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("login", "blive1"));
    nameValuePairs.add(new BasicNameValuePair("password",
            "d30a62033c24df68bb091a958a68a169"));
    post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute request
    HttpResponse response = httpclient.execute(post);

    // Check response status and read data
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        String data = EntityUtils.toString(response.getEntity());
    }
} catch (Exception e) {
    System.out.println(e);
}
aioobe
  • 413,195
  • 112
  • 811
  • 826
Perception
  • 79,279
  • 19
  • 185
  • 195
  • Something like: `HttpPut httpPut = new HttpPut(BASE_URI + "/services.users/login");` `Form f = new Form(); f.add("login", "xxxxx"); f.add("password", "xxxxx");` ?? – user2144555 Mar 21 '13 at 11:05
  • @user2144555 - See edits for a viable way to call the server resource, using Apache HttpComponents. – Perception Mar 21 '13 at 11:13
  • I was using Apache HttpClient too. But I was thinking that " **form variables for login and password** " were send in a different way. I will try that! – user2144555 Mar 21 '13 at 11:16
  • Thanks, it's working! But I was initially asking how to enter those parameters in the TEST RESTful WEB SERVICES main page. Do you know what I'm talking about? It's their main page. – user2144555 Mar 21 '13 at 11:24
  • Whats the link of the page you are talking about? – Perception Mar 21 '13 at 11:27
  • C:\Users\Ines\Documents\NetBeansProjects\LULServices\build\generated-sources\rest-test\test-resbeans.html – user2144555 Mar 21 '13 at 11:33
  • I would guess that to be a Netbeans auto-generated test page for your web service. Unfortunately, it doesn't seem like Netbeans could detect the invalid combination of annotations on your resource method. – Perception Mar 21 '13 at 12:04
  • Sorry, another question about the @FormParam: they can be used with PUTs? I'm always doing PUTs instead of POSTs. Is that a problem? – user2144555 Mar 21 '13 at 15:51
  • You should only do PUT's for operations that will be updating an entity. Actually, each one of the HTTP verbs maps to a specific RESTful operation, and you should try to use them appropriately. You can read [this StackOverflow answer](http://stackoverflow.com/a/2022938/680925), which gives a nice summary of what to use when. – Perception Mar 21 '13 at 16:29
  • Yes, I'm always doing updates. But i read that "HTML forms are an easy means of getting information from a user and they are also easy to create. Form data can be used for HTTP GET requests and HTTP POST requests." That's why I was asking. – user2144555 Mar 21 '13 at 16:38
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26660/discussion-between-perception-and-user2144555) – Perception Mar 21 '13 at 16:39