Manu
Manu

Reputation: 1369

Error while calling RESTful Web-Service using Jersey Client POST method

Getting an error while trying to consume a Restful web service using POST method(with form param).

I want to consume a REST application using POST method. Please find below the resource class I want to access.

@Path("/user")

public class User {

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response get(@FormParam("username") String userName,
        @FormParam("userid") String userId  ){

}

I tried using Jesry Client for accessing.Please find below the code i tried. I tried adding values to FormParam as shown below.

Trail 1

WebResource webResource = client.resource("baseURL/user");

String input = "userid:1001,username:demo1";
ClientResponse response = webResource.type("application/x-www-form-urlencoded").post(ClientResponse.class, input);

I am getting a an error response back "The server encountered an internal error () that prevented it from fulfilling this request". I think I am not adding the values to FormParam properly.

Trial 2

I also tried adding the form params using the below code

MultivaluedMap formData = new MultivaluedMapImpl();
    formData.add("userid", "1001");
    formData.add("username", "demo1");
    ClientResponse response = webResource.type("application/x-www-form-urlencoded").post(ClientResponse.class, formData);

This also resulted in the same error.

Trial 3

Form f = new Form();
    f.add("userid", "1001D");
    f.add("username", "1001D");

    ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).post(ClientResponse.class, f);

This also resulted in the same error. Any help is appreciated.

Upvotes: 1

Views: 2095

Answers (2)

Paul Jowett
Paul Jowett

Reputation: 6581

Since your error indicates "Server encountered an internal error" you need to look at the server (logs) to see what went wrong. Certainly your 3rd client looks fine to reach the service you defined (assuming you are using something real instead of the string "baseURL").

You can easily test your server is working separately from your client by creating a HTML page to reach the service. Create a HTML form using enctype="application/x-www-form-urlencoded" and posting to your service endpoint (what you are calling "baseURL/user") with form parameters userid and username. When you view the HTML form in a browser and hit the submit button, it will call your server - if you get the same error you can be sure it is nothing to do with your client code.

Upvotes: 1

Related Questions