Skip to content

My take on a simple HTTP Façade for easy request making.

License

Notifications You must be signed in to change notification settings

luanpotter/http-facade

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

90 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

http-facade

Maven Central GitHub release License

My take on a simple HTTP Façade for easy request making.

Zero-dependency, unobtrusive, Java 7/Google App Engine-ready API for some pretty HTTP handling.

Maven

<dependency>
    <groupId>xyz.luan</groupId>
    <artifactId>http-facade</artifactId>
    <version>2.5.1</version>
</dependency>

Examples

Simple get:

    Response r = new HttpFacade("www.google.com").get();
    r.status() // 200
    r.content() // <html>...

More complex request:

    new HttpFacade("luan.xyz/api/people")
        .header("key", "value")
        .body("{ id: 42, name: \"Luan\" }")
    .post();

Parse URL's, set form and query params, handle cookies, authentication (built-in non-platform-dependent Base64 enc/dec), and more.

For problems with SSL requests and Java outdated CA certificate repositories, see here.

Mocking Requests

If you want to mock a request in order to always retrieve a specific response, instead of actually making the request, you can use MockedHttpFacade and MockedResponse.

Simply inject/instantiate a mocked service to do the request; see the following example:

    class MyService {

        public void sendRequest(String url) {
            getFacade(url).get();
        }

        public HttpFacade getFacade(String url) {
            return new HttpFacade(url);
        }
    }

    class MockMyService extends MyService {
        @Override
        public HttpFacade getFacade(String url) {
            MockedResponse.build().withStatus(200).withContent("{ name : mock }");
            return new MockedHttpFacade(url).mockResponse(mock);
        }
    }