Marko Otasevic
Marko Otasevic

Reputation: 7

RESTful client in order to test my POST method

I am working on the RESTful cloud application in java and once I implemented all the methods for POST, DELETE, PUT and GET, i need to test them through a simple java client. I successfully tested the GET and DELETE in the following manner:

String findById = target.path("..").path("..").path("17").request()
.accept(MediaType.APPLICATION_JSON).get(String.class).toString();
System.out.println(findById);

But now when I want to try the PUT and POST, I can not make it work. I tried the following:

String addBook = target.path("..").path("..").queryParam("author", "OO")
                .queryParam("language", "OO")
                .queryParam("publisher", "OO")
                .queryParam("subject", "OO")
                .queryParam("title", "OO")
                .queryParam("isbn", "OO").request().accept(MediaType.APPLICATION_JSON)
                .post(String.class);

The error says that .post is expecting "The method post(Entity, Class) in the type SyncInvoker is not applicable for the arguments (String, Class)".

So my question is what the Entity should be and how should I specify it? Client project is running separately and not on the main(server) project.

Upvotes: 0

Views: 1927

Answers (1)

Nicolò Boschi
Nicolò Boschi

Reputation: 138

You cannot use query param to send a post request. You have to use a post body, that can be in different content types, like JSON. You have to change your code to something like this:

Map<String, Object> body = new HashMap<>();
body.put("language", "OO");
.....
String json = (new ObjectMapper()).writeValueAsString(body);
Entity<String> entity = javax.ws.rs.client.Entity.json(json);
String result = client.target(fullUrl).request().method("POST", entity, String.class);

Upvotes: 1

Related Questions