Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

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

README.md

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.4.0</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);
        }
    }
You can’t perform that action at this time.