I am using the RESTEasy client framework to call a RESTful webservice. The call is made via a POST and sends some XML data to the server. How do I accomplish this?
What is the magical incantation of annotations to use to make this happen?
Try this syntax:
Form form = new Form();
form
.param("client_id", "Test_Client")
.param("grant_type", "password")
.param("response_type", "code")
.param("scope", "openid")
.param("redirect_uri", "some_redirect_url");
Entity<Form> entity = Entity.form(form);
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("http://localhost:8080/auth/realms");
Response response = target
.request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, authCreds)
.post(entity);
System.out.println("HTTP code: " + response.getStatus());
It is as easy as following
@Test
public void testPost() throws Exception {
final ClientRequest clientCreateRequest = new ClientRequest("http://localhost:9090/variables");
final MultivaluedMap<String, String> formParameters = clientCreateRequest.getFormParameters();
final String name = "postVariable";
formParameters.putSingle("name", name);
formParameters.putSingle("type", "String");
formParameters.putSingle("units", "units");
formParameters.putSingle("description", "description");
formParameters.putSingle("core", "true");
final ClientResponse<String> clientCreateResponse = clientCreateRequest.post(String.class);
assertEquals(201, clientCreateResponse.getStatus());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With