https://github.com/Vatavuk/verano-http
Java HTTP Client in OOP style
https://github.com/Vatavuk/verano-http
Last synced: 3 months ago
JSON representation
Java HTTP Client in OOP style
- Host: GitHub
- URL: https://github.com/Vatavuk/verano-http
- Owner: Vatavuk
- License: mit
- Created: 2019-02-21T14:20:10.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-07T19:38:47.000Z (over 3 years ago)
- Last Synced: 2026-01-22T22:21:52.065Z (6 months ago)
- Language: Java
- Homepage:
- Size: 334 KB
- Stars: 54
- Watchers: 4
- Forks: 6
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-java - Verano HTTP
README

[](http://www.elegantobjects.org)
[](http://www.rultor.com/p/Vatavuk/verano-http)
[](https://travis-ci.org/Vatavuk/verano-http)
[](https://codecov.io/gh/Vatavuk/verano-http)
[](https://hitsofcode.com/view/github/Vatavuk/verano-http)
[](https://sonarcloud.io/dashboard/index/hr.com.vgv.verano:verano-http)
[](http://javadoc.io/doc/hr.com.vgv.verano/verano-http)
[](https://maven-badges.herokuapp.com/maven-central/hr.com.vgv.verano/verano-http)
[](https://github.com/Vatavuk/verano-http/blob/master/LICENSE.txt)
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](#testing) without mock servers which gives a performance edge.
Similar libraries: [cactoos-http](https://github.com/yegor256/cactoos-http), [jcabi-http](https://github.com/jcabi/jcabi-http)
C# port of the library: [Yaapii.Http](https://github.com/icarus-consulting/Yaapii.Http) ([icarus-consulting](https://github.com/icarus-consulting))
```xml
hr.com.vgv.verano
verano-http
1.1
```
## Get a Url
```java
JsonObject json = new JsonBody.Of(
new Response(
"http://example.com",
new Get(
"/items",
new QueryParam("name", "John"),
new Accept("application/json")
)
)
).json();
```
## Post to a Server
```java
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:
```java
new Response(
"http://example.com",
new Post(
"/items",
new FormParam("name","John"),
new FormParam("foo","bar"),
)
).touch();
```
## Response handling
Extraction of response parameters can be done using `*.Of` classes:
```java
Response response = new Response(
"http://example.com",
new Get("/items")
);
Map> 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:
```java
new Response(
"http://exmpl.com",
new Get("/items"),
new ExpectedStatus(
200,
new FailWith("Cannot fetch from exmpl")
)
).touch();
```
## Serialization and Deserialization
The library provides following types of request body serialization:
- [JsonBody](https://github.com/Vatavuk/verano-http/blob/master/src/main/java/hr/com/vgv/verano/http/parts/body/JsonBody.java#L42) - javax.json
- [XmlBody](https://github.com/Vatavuk/verano-http/blob/master/src/main/java/hr/com/vgv/verano/http/parts/body/XmlBody.java#37) - [jcabi-xml](https://github.com/jcabi/jcabi-xml)
- [DtoBody](https://github.com/Vatavuk/verano-http/blob/master/src/main/java/hr/com/vgv/verano/http/parts/body/DtoBody.java#37) - jackson object mapper
- [HtmlBody](https://github.com/Vatavuk/verano-http/blob/master/src/main/java/hr/com/vgv/verano/http/parts/body/HtmlBody.java#37) - [jsoup](https://github.com/jhy/jsoup)
Response body deserialization can be achieved using their accompanied `*.Of` classes.
## Wire
Verano-http runs on `ApacheWire` which encapsulates [Apache http client](https://github.com/apache/httpcomponents-client).
You can provide additional configuration to the wire:
```java
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:
```java
new Response(
new ApacheWire(
"http://exmpl.com",
new ContentType("application/json"),
new Header("foo", "bar"),
),
new Get("/items")
).touch();
```
## Testing
Http requests can be tested through `MockWire` without using a http server.
`MockWire` works in conjunction with [hamcrest matchers](http://hamcrest.org/JavaHamcrest/) in a following way:
```java
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"))
)
);
```
## Contribution
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
```