5

I know I can add http parameters using the setParameter method, but how do I pass a body to the http request using the URIBuilder class?

For example, this

URI uri = new URIBuilder().setScheme("http")
                .setHost("localhost:9091/test").setParameter("a", "1")
                .setParameter("b", "2").build();

is equivalent to the following curl request:

curl -X POST http://localhost:9091/test\?a\=1\&b\=2

but how do I build a URL using URIBuilder (or any other class) for the following curl:

curl -X POST http://localhost:9091/test -d '{"a":1,"b":2}'

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
gravetii
  • 9,273
  • 9
  • 56
  • 75

1 Answers1

8
HttpUriRequest request = RequestBuilder.create("POST")
    .setUri("http://localhost:9091/test")
    .setEntity(new StringEntity("{\"a\":1,\"b\":2}", ContentType.APPLICATION_JSON))
    .build();
ok2c
  • 26,450
  • 5
  • 63
  • 71