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?
Asked
Active
Viewed 68 times
0

justlikethat
- 329
- 2
- 12
-
4You should take the developers of that api out back and shoot them. – Code-Apprentice Aug 25 '22 at 23:32
-
See https://stackoverflow.com/questions/978061/http-get-with-request-body (doesn't answer your question, just context) – tgdavies Aug 26 '22 at 00:02
-
thanks for the responses, but is there a solution? – justlikethat Aug 26 '22 at 00:35
1 Answers
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