HTTP client that provides object-oriented interface for building HTTP requests. The benefits of this library are that it is easily customizable, declarative and immutable. It can also be tested without mock servers which gives a performance edge.
Similar libraries: cactoos-http, jcabi-http
C# port of the library: Yaapii.Http (icarus-consulting)
<dependency>
<groupId>hr.com.vgv.verano</groupId>
<artifactId>verano-http</artifactId>
<version>1.1</version>
</dependency>
JsonObject json = new JsonBody.Of(
new Response(
"http://example.com",
new Get(
"/items",
new QueryParam("name", "John"),
new Accept("application/json")
)
)
).json();
new Response(
"http://example.com",
new Post(
"/items",
new Body("Hello World!"),
new ContentType("text/html"),
)
).touch();
Touch
method executes the HTTP
request towards the server.
Using form parameters:
new Response(
"http://example.com",
new Post(
"/items",
new FormParam("name","John"),
new FormParam("foo","bar"),
)
).touch();
Extraction of response parameters can be done using *.Of
classes:
Response response = new Response(
"http://example.com",
new Get("/items")
);
Map<String, List<String>> headers = new Headers.Of(response).asMap(); // Extraction of headers from response
String cookie = new Cookie.Of("cookieName", response).asString();
String body = new Body.Of(response).asString();
You can make assertions on received responses like this:
new Response(
"http://exmpl.com",
new Get("/items"),
new ExpectedStatus(
200,
new FailWith("Cannot fetch from exmpl")
)
).touch();
The library provides following types of request body serialization:
Response body deserialization can be achieved using their accompanied *.Of
classes.
Verano-http runs on ApacheWire
which encapsulates Apache http client.
You can provide additional configuration to the wire:
Wire wire = new ApacheWire(
"http://exmpl.com",
new Proxy("127.0.0.1", 8000),
new SslTrusted()
);
new Response(
wire, new Get("/items")
).touch();
You can also provide http parameters to the wire:
new Response(
new ApacheWire(
"http://exmpl.com",
new ContentType("application/json"),
new Header("foo", "bar"),
),
new Get("/items")
).touch();
Http requests can be tested through MockWire
without using a http server.
MockWire
works in conjunction with hamcrest matchers in a following way:
MockWire wire = new MockWire(
new MockAnswer(
new PathMatch(MatchesPattern.matchesPattern("/.*")),
new Response(new Status(201))
)
);
sendRequest(wire);
wire.verify(
new PostMatch(
new PathMatch(new IsEqual<>("/items")),
new BodyMatch(new StringContains("text"))
)
);
You can contribute by forking the repo and sending a pull request. Make sure your branch builds without any warnings/issues:
mvn clean install -Pqulice