0

We are consuming an API and we are required to send filter values in the payload while making GET calls(not in the URL). Is there a library that supports this?

justlikethat
  • 329
  • 2
  • 12

1 Answers1

1

If you using java 11 or above, I don`t think you need other library, you can send body with java.net.http.HttpClient

import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;

public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {

    HttpRequest request = HttpRequest.newBuilder()
            .uri(new URI("http://localhost:8001"))
            .method("GET", HttpRequest.BodyPublishers.ofString("Sample request body"))
            .build();
    HttpResponse<String> response = HttpClient.newBuilder()
            .build()
            .send(request, BodyHandlers.ofString());
}


// server side print **with body**
// b'GET / HTTP/1.1\r\nConnection: Upgrade, HTTP2-Settings\r\nContent-Length: 19\r\nHost: localhost:8001\r\nHTTP2-Settings: AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA\r\nUpgrade: h2c\r\nUser-Agent: Java-http-client/16.0.2\r\nContent-Type: text/plain;charset=UTF-8\r\n\r\nSample request body'

Hi computer
  • 946
  • 4
  • 8
  • 19