Skip to content

luanpotter/http-facade

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
doc
 
 
src
 
 
 
 
 
 
 
 
 
 

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);
        }
    }