0

I'm trying to consume an external api exposed a payment provider. I user Jersey and javax.ws.rs for request, because I can easily send authent with Digest.

But when it comes to make the request, a GET with payload, Jersey returns

> IllegalStateException. Entity must be null for http method GET
CashTransactionResponse responseData = null;
//We connect to intouch server
String requestUrl = rootUrlTouchPay + agency.getAgencyCode() + "/" + IntouchMethodApis.TRANSACTION + "?loginAgent=" + agency.getLogin() + "&passwordAgent=" + agency.getPassword();

ClientConfig clientConfig = new ClientConfig();
//Open Digest authentication
HttpAuthenticationFeature feature = HttpAuthenticationFeature.digest(BASIC_LOGIN, BASIC_PWD);
clientConfig.register(feature);

clientConfig.register(JacksonFeature.class);
//Create new rest client
Client client = ClientBuilder.newClient(clientConfig);
clientConfig.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
//Set the url
WebTarget webTarget = client.target(requestUrl);
Invocation.Builder invocationBuilder = webTarget.request(javax.ws.rs.core.MediaType.APPLICATION_JSON);

logger.info("Initialisation of cashout service successful for cash");

// create request
Gson gson = new Gson();
String transactionString = gson.toJson(cashRequest);

Response response = null;
// start the response
if (cashRequest.getServiceCode().contains(TelecomEnum.WAVE.name().toUpperCase())) {
    response = invocationBuilder.method("GET", Entity.entity(transactionString, javax.ws.rs.core.MediaType.APPLICATION_JSON));
} else {
    response = invocationBuilder.put(Entity.entity(transactionString, javax.ws.rs.core.MediaType.APPLICATION_JSON));
}

Please how could i do to send my GET request with body ?

Thanks

Kaan
  • 5,434
  • 3
  • 19
  • 41
Teddy Kossoko
  • 1,168
  • 3
  • 20
  • 48

2 Answers2

0

Instead of doing an invocationBuilder.method("GET", ...), use invocationBuilder.post(entity), as described here. This will allow you to POST your transaction String to the endpoint.

Erik Pragt
  • 13,513
  • 11
  • 58
  • 64
  • the remote server is waiting for GET that's why i send a GET. – Teddy Kossoko Jun 30 '22 at 09:25
  • that's fine, but then send the GET without a body. – Erik Pragt Jun 30 '22 at 13:49
  • the body must contain some data they use to process my request on server side. – Teddy Kossoko Jul 01 '22 at 14:02
  • I think there's a misunderstanding on your end. You either send the GET without the body, or a POST with body. So, why in theory you could send a body with a GET request, [it's never useful to do so](https://stackoverflow.com/questions/978061/http-get-with-request-body). – Erik Pragt Jul 02 '22 at 07:37
0

I believe the problem is in

Client client = ClientBuilder.newClient(clientConfig); clientConfig.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);

The client copied the values from clientConfig and any further settings on clientConfig do not have any impact on the client.

Either switch the lines or set the ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION property on the client.

jan.supol
  • 2,636
  • 1
  • 26
  • 31