A simple way to build and configure a HTTP client and work with the HTTP verbs;
HttpResponse response = anApacheClient().get(url,
headers(
header("Accept", "application/json")
)
);
Will create a HTTP client, perform a GET
with an Accept
header and populate a simple HttpResponse
object. Simple-HTTP currently wraps the Apache HTTP client but does so in a API agnostic way. Think of it as a smaller, simpler way to use Apache without all the boiler plate code.
See this blog post for an overview.
- No noise. Build and work with HTTP clients simply.
- Supports the basic HTTP verbs (
GET
,POST
,PUT
,DELETE
). - Easy to configure. Comes out of the box with sensible defaults but is easy to customise.
- Supplied with Hamcrest
Matcher
s making testing your clients straight forward. - Supports typical HTTP settings; SSL, follow redirects, timeouts, proxies etc.
HttpClient http = HttpClients.anApacheClient();
That's all there is to it. Now start making HTTP requests,
http.get(new URL("http://baddotrobot.com"));
and if you want to make the request with specific headers,
http.get(new URL("http://baddotrobot.com"),
headers(
header("Accept", "application/json"),
));
You can set things up to go throw a proxy (e.g. localhost:8888
) like this.
anApacheClient().with(proxy(new URL("http://username:password@localhost:8888"))).get(new URL("http://baddotrobot.com"));
You could add the Authorization
header to your requests manually or just do the following,
AuthorisationCredentials credentials = basicAuth(username, password, new URL("http://example.com")));
anApacheClient().with(credentials).get(new URL("http://example.com/something"));
This will add basic auth headers, similar to below
Authorization: Basic aGVsbG86d29ybGQNCg==
to every request made to http://example.com
(but not to other URLs).
In a similar way to the basic auth headers above, you can add Bearer
authorisation,
AuthorisationCredentials credentials = oAuth(accessToken("XystZ5ee"), new URL("http://example.com"));
HttpResponse response = anApacheClient().with(credentials).get(new URL("http://example.com/foo"));
This will add the
Authorization: Bearer XystZ5ee
header to every request to http:\\example.com
.
You can even mix the schemes
AuthorisationCredentials basicAuthCredentials = basicAuth(username("username"), password("secret"), new URL("http://robotooling.com"));
AuthorisationCredentials oAuthCredentials = oAuth(accessToken("XystZ5ee"), new URL("http://baddotrobot.com"));
HttpClient http = anApacheClient().with(basicAuthCredentials).with(oAuthCredentials);
http.get(new URL("http://baddotrobot.com"));
http.get(new URL("http://robotooling.com"));
which will use basic auth for requests to http://robotooling.com
and bearer token auth for requests to http://baddotrobot.com
.
Available via the Maven repository.