I wrote this simple Http client to use in my AppEngine applications that use the urlFetch service. Because it runs in AppEngine, it can only use the native Java classes (in java.net). The goal was to make a very simple Http client that I can use to call external RESTful API's. I wanted to hide all of the complexities behind an intuitive interface.
I also used generics so that the method calls can be chained together.
Here's an example of a simple GET request:
Response httpResponse = new Request("http://example.com")
.getResource();
String responseBody = httpResponse.getBody();Here's a GET request with a header and a query parameter specified:
Response httpResponse = new Request("http://example.com")
.addHeader("x-my-header", "foobar")
.addQueryParameter("foo", "bar")
.getResource();
String responseBody = httpResponse.getBody();Here's a more complicated example of a POST request with query parameters and a Content-Type header:
// Posts a simple JSON object to the server
Response httpResponse = new Request("http://example.com")
.addHeader("Content-Type", "application/json")
.addQueryParameter("foo", "bar")
.setBody("{foo: 'bar'}")
.postResource();
String responseBody = httpResponse.getBody();