2

I know that GET call should not have body but the call is developed by other people and I can't change it now. I want to consume an API which is GET method and takes payload (json body). I can consume a GET method passing path param but not payload. I don't see an option to send payload for GET call.

Here is the GET call I am doing.

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

Client client = ClientBuilder.newClient();
String targetUri = "http://" + service.getHost() + ":" + service.getPort() + PROFILES_URI;
Response response = client
            .target(target)
            .path(profileIds.get(0))
            .request(MediaType.APPLICATION_JSON)
            .get();

If the method is PUT or POST I can send the payload as shown below.

Client client = ClientBuilder.newClient();
String target = "http://" + service.getHost() + ":" + service.getPort() + PROFILES_URI;
Response response = client
            .target(target)
            .request(MediaType.APPLICATION_JSON)
            .post(Entity.entity(profileIds, MediaType.APPLICATION_JSON));

How do I send payload with GET call?

Reference: http://www.baeldung.com/jersey-jax-rs-client

chom
  • 371
  • 4
  • 20
  • 1
    GET call can't have a body. – Gusman Aug 02 '17 at 16:48
  • 1
    GET requests cannot have a payload. – SLaks Aug 02 '17 at 16:48
  • 2
    You need to do a POST – OldProgrammer Aug 02 '17 at 16:48
  • I can't change the GET call. It is written by other people and I can only consume it. – chom Aug 02 '17 at 17:01
  • @AravindR Did you even read my question. I am not developing a rest call here. I am consuming the call. Please don't just comment for points. – chom Aug 02 '17 at 17:06
  • I dint had any such intentions, i feel the requirement itself is little bizzare. Anyways here are a few i found that could help you https://stackoverflow.com/questions/23645878/send-json-data-in-an-http-get-request-to-a-rest-api-from-java-code https://stackoverflow.com/questions/15202300/httpget-with-request-body-android https://www.quora.com/How-can-I-send-request-body-with-http-GET-request-in-java – Aravind R Aug 02 '17 at 18:09
  • @chom _Please don't just comment for points._ Comments don't give any reputation. – cassiomolin Aug 03 '17 at 15:15

1 Answers1

3

I am not developing a rest call here

Actually you are, because javax.ws.rs is the base package of RESTful web services in Java and javax.ws.rs.client.Client is the base interface for RESTfull web service clients: Overview of the Client API.

That being said you'll probably need to either build your own client that allows you to send a payload in a GET request or find an existent web service client with such capability.

The correctness of sending a body in a GET request is actually a different topic already discussed in this Q&A: HTTP GET with request body

dic19
  • 17,446
  • 6
  • 35
  • 64