Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use RESTEasy client framework to send data in a POST

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?

like image 508
David Escandell Avatar asked May 20 '10 21:05

David Escandell


2 Answers

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());
like image 156
d0wn Avatar answered Nov 02 '22 23:11

d0wn


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());
    }
like image 39
daydreamer Avatar answered Nov 02 '22 23:11

daydreamer