Hello,
I’ve tried this tutorial (http://www.myeclipseide.com/documentation/quickstarts/webservices_rest/) to develop a RESTful Web service. To test the Web service I wrote the following client.
import …
public class TestService {
public static void main(String[] args) {
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
//test Get
ClientResponse response = service.path(“services”).path(“customers”).accept(“application/xml”).get(ClientResponse.class);
System.out.println(response.toString());
System.out.println(“Response body: “+response.getEntity(String.class);
System.out.println(“Response size: “+ textEntity.getBytes().length + ” Bytes”);
//test Post
//create a form
//Form formData = new Form();
//formData.add(“address”, “address2”);
//formData.add(“name”,”client2″);
ClientResponse response1 = service.path(“services”).path(“customers”).path(“add”).accept(“application/xml”).post(ClientResponse.class,formData);
System.out.println(response1.toString());
}
private static URI getBaseURI() {
return UriBuilder.fromUri(“http://localhost:8081/restdemo/”).build();
}
}
The test for GET was ok, but POST did not, and returned the http error code 415. Seems I could not build the correct object “formData” (in my example) to pass it with the POST method. However, the POST method is working fine when I use the poster add-on for firefox and records successfully added to my web service.
Could you please help me on how to build the correct object with post in this case?
Thanks,
Khalid