Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed Jersey client api way for posting a webrequest with json payload and headers
    text
    copied!<p>I am writing a client for one of my REST API using jersey(org.glassfish.jersey.client.*).</p> <p>api url is : <code>http://localhost:5676/searchws/search/getresults</code> (POST)</p> <p>this api returns a json response. i need to provide a payload using jersey client and thats where i am stuck. FOllowing is a sample extract of payload which i need to provide (preferably as string)</p> <p>Question is how can i provide a payload (XML/JSON) as string or entity to my webtarget.</p> <p>I saw the answer to providing payload mentioned by calden <a href="https://stackoverflow.com/questions/15570656/how-to-send-request-payload-to-rest-api-in-java">How to send Request payload to REST API in java?</a> but i am looking for a way to do it in jersey client.</p> <p>Here is my code till now which does not work fully for post requests.</p> <pre><code>public class RequestGenerator { private WebTarget target; private ClientConfig config; private Client client; private Response response; public RequestGenerator(Method RequestSendingMethod) throws Exception { switch (RequestSendingMethod) { case POST : config = new ClientConfig(); client = ClientBuilder.newClient(config); target = client.target("http://localhost:5676/searchws").path("search").path("getresults"); String payload = "{\"query\":\"(filter:(\\\"google\\\")) AND (count_options_availbale:[1 TO *])\"}"; //This is just a sample json payload actual one is pretty large response = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json("")); // What to do here String jsonLine = response.readEntity(String.class); System.out.println(jsonLine); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload