Skip to content

Commit b3fa2ad

Browse files
committed
Update README.md
1 parent ac1bd03 commit b3fa2ad

File tree

1 file changed

+112
-1
lines changed

1 file changed

+112
-1
lines changed

README.md

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,112 @@
1-
# avaje-http-client
1+
# avaje-http-client
2+
3+
Create a HttpClientContext with a baseUrl, Jackson or Gson based JSON
4+
body adapter, logger.
5+
6+
```java
7+
public HttpClientContext client() {
8+
return HttpClientContext.newBuilder()
9+
.withBaseUrl(baseUrl)
10+
.withResponseListener(new RequestLogger())
11+
.withBodyAdapter(new JacksonBodyAdapter(new ObjectMapper()))
12+
// .withBodyAdapter(new GsonBodyAdapter(new Gson()))
13+
.build();
14+
}
15+
16+
```
17+
18+
From HttpClientContext:
19+
- Create a request
20+
- Build the url via path(), matrixParam(), queryParam()
21+
- Optionally set headers(), cookies() etc
22+
- Optionally specify a request body (JSON, form, or raw BodyPublisher)
23+
- Http verbs - get(), post(), put(), delete()
24+
- Optionally return response body as a bean, list of beans, or raw
25+
26+
## Examples
27+
28+
GET as String
29+
```java
30+
final HttpResponse<String> hres = clientContext.request()
31+
.path("hello")
32+
.get().asString();
33+
34+
```
35+
36+
GET as json to single bean
37+
```java
38+
final HelloDto bean = clientContext.request()
39+
.path("hello/there")
40+
.get().bean(HelloDto.class);
41+
```
42+
43+
POST a bean as json request body
44+
```java
45+
HelloDto bean = new HelloDto(12, "rob", "other");
46+
47+
final HttpResponse<Void> res = clientContext.request()
48+
.path("hello/savebean")
49+
.body(bean).post()
50+
.asDiscarding();
51+
52+
assertThat(res.statusCode()).isEqualTo(201);
53+
54+
```
55+
56+
GET as json to list of beans
57+
```java
58+
final List<HelloDto> beans = clientContext.request()
59+
.path("hello")
60+
.get().list(HelloDto.class);
61+
```
62+
63+
Path
64+
```java
65+
final HttpResponse<String> res = clientContext.request()
66+
.path("hello")
67+
.path("withMatrix")
68+
.path("2011")
69+
.get().asString();
70+
71+
// is the same as ...
72+
73+
final HttpResponse<String> res = clientContext.request()
74+
.path("hello/withMatrix/2011")
75+
.get().asString();
76+
```
77+
78+
MatrixParam
79+
```java
80+
final HttpResponse<String> httpRes = clientContext.request()
81+
.path("hello")
82+
.matrixParam("author", "rob")
83+
.matrixParam("country", "nz")
84+
.path("foo")
85+
.matrixParam("extra", "banana")
86+
.get().asString();
87+
```
88+
89+
QueryParam
90+
```java
91+
final List<HelloDto> beans = clientContext.request()
92+
.path("hello")
93+
.queryParam("sortBy", "name")
94+
.queryParam("maxCount", "100")
95+
.get().list(HelloDto.class);
96+
```
97+
98+
FormParam
99+
```java
100+
final HttpResponse<Void> res = clientContext.request()
101+
.path("hello/saveform")
102+
.formParam("name", "Bazz")
103+
.formParam("email", "user@foo.com")
104+
.formParam("url", "http://foo.com")
105+
.formParam("startDate", "2020-12-03")
106+
.post()
107+
.asDiscarding();
108+
109+
assertThat(res.statusCode()).isEqualTo(201);
110+
```
111+
112+
## Currently NO support for POSTing multipart-form

0 commit comments

Comments
 (0)