Reputation: 5430
I want to make an HTTP POST
request, which requires parameters must be passed as raw JSON text, but I don't know how to do in with Jersey client.
Here is the API specs (works well in Postman Rest Client): (please look at the screenshot from my Postman, the URL, parameter in body, and 2 headers are Content-Type: application/json & Accept: application/json)
And here is what I've tried with Jersey:
JsonObject parameters = new JsonObject();
parameters.addProperty("centerId", centerId);
parameters.addProperty("studentId", studentId);
if (fromDate != null) {
parameters.addProperty("fromDate", fromDate);
}
if (toDate != null) {
parameters.addProperty("toDate", toDate);
}
Object response = client.target("http://localhost:8080/rest").path("student/calendar")
.request(MediaType.APPLICATION_JSON).post(Entity.json(parameters), String.class);
but nothing worked. Can anyone suggest what is the correct way to deal with Jersey in this case? Thank you.
Upvotes: 5
Views: 8160
Reputation: 16833
Use an InputStream
for your entity construction, it should work
String payload = "{\"schoolId\":1,\"studentId\":1,\"fromDate\":\"1454259600000\",\"toDate\":\"1456765200000\"}";
client.target("<targetURI>")
.request(MediaType.APPLICATION_JSON)
.post(Entity.entity(
new ByteArrayInputStream(payload.getBytes()),
MediaType.APPLICATION_JSON
));
But in the end, Entity.json(payload)
should also work.
Upvotes: -1