All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ca.bitcoco.jsk.http.README.md Maven / Gradle / Ivy

HTTP Client Builder Design pattern

```java
new HttpClient()
.URL()
.method()
.addHeader()
.JSONBody()
.postParams()
.retry()
.requestConfig()
.send()
```

### Usage in code
```java

            // how to make a post in form format
            HashMap formParams = new HashMap<>();
            formParams.put("identifier", "XXXXX");
            formParams.put("password", "XXXXX");
            HttpClientResponse response = new HttpClient()
                    .method(HttpMethod.POST)
                    .URL("http://stage.us.autobestinfo.com/api/auth/login")
                    .postParams(formParams)
                    .send();
            if (response != null) {
				System.out.println("protocolV  :" + response.getProtocolVersion());
				System.out.println("Duration   :" + response.getDuration() + "ms");
				System.out.println("Status Code:" + response.getStatusCode());
				System.out.println("Body       :" + response.getBody());
            }

           // how to make a post in json body
            StringBuilder json = new StringBuilder();
            json.append("{");
            json.append("\"identifier\":\"XXXXX\",");
            json.append("\"password\":\"XXXXX\"");
            json.append("}");
            HttpClientResponse response1 = new HttpClient()
                    .method(HttpMethod.POST)
                    .URL("http://stage.us.autobestinfo.com/api/auth/login")
                    .JSONBody(json.toString())
                    .send();
            if (response1 != null) {
				System.out.println("protocolV  :" + response1.getProtocolVersion());
				System.out.println("Duration   :" + response1.getDuration() + "ms");
				System.out.println("Status Code:" + response1.getStatusCode());
				System.out.println("Body       :" + response1.getBody());
            }

            // how to make a GET with headers(optional)
            String token = "Bearer XXXXXeyJqdGkiOiJlMWNiYjg5Ny1jNWFmLTQ1MzgtOWYyMS1kNmI5NjQyMzIyMGEiLCJzdWIiOiJhZG1pbiIsImlhdCI6MTYxMjQwODUxOCwiZXhwIjoxNjEzMjcyNTE4fQ.9vz0y_oY00yqK38_NN3Yyc4ESwMpsyWTCZsPYr48Bo13M0_v3X2Nbf2dirCVkCN05BBDHtIomQzBp58FGW01cg";
            HttpClientResponse response2 = new HttpClient()
                    .method(HttpMethod.GET)
                    .URL("http://stage.us.autobestinfo.com/api/auth/v1/users/me")
                    .addHeader(HttpHeader.AUTHORIZATION, token)
                    .send();
            if (response2 != null) {
                System.out.println("protocolV  :" + response2.getProtocolVersion());
                System.out.println("Duration   :" + response2.getDuration() + "ms");
                System.out.println("Status Code:" + response2.getStatusCode());
                System.out.println("Body       :" + response2.getBody());
            }

           // how to make a request with retry(optional)
           //retry only when return null or get exception in http request
            HttpClientResponse response3 = new HttpClient()
                    .method(HttpMethod.GET)
                    .retry(3)
                    .URL("http://stage.us.autobestinfo.com/api/bs/v1/rideshare/drivers?limit=10&page=1&searchObj=%7B%22$or%22:%5B%7B%22departure_area.city%22:%22Ottawa%22%7D,%7B%22arrival_area.city%22:%22Ottawa%22%7D%5D%7D&sortObj=%7B%7D")
                    .send();
            if (response3 != null) {
                System.out.println("protocolV  :" + response3.getProtocolVersion());
                System.out.println("Duration   :" + response3.getDuration() + "ms");
                System.out.println("Status Code:" + response3.getStatusCode());
                System.out.println("Body       :" + response3.getBody());
            }
```

### result print out
```text
Demo:POST with Form ===
protocolV  :HTTP/1.1
Duration   :241ms
Status Code:401
Body       :

Demo: POST with JSON ===
protocolV  :HTTP/1.1
Duration   :452ms
Status Code:200
Body       :
{
    "auth": "Bearer eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiI4OWMwMDgwYS1iNGIwLTRmYWQtYjMxOC0wODM0ODM4ZjEwNmIiLCJzdWIiOiJhZG1pbiIsImlhdCI6MTYxMjQ4MDk2MiwiZXhwIjoxNjEzMzQ0OTYyfQ.DED0cxCRWXMjkXyaEMW4ztjDJP1J2dkxCOiHktINFoDmnkshzVMcfs2GA3XeyoO30BU6qCjE1hFGh08EwwSiDQ",
    "access_token": "eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiI4OWMwMDgwYS1iNGIwLTRmYWQtYjMxOC0wODM0ODM4ZjEwNmIiLCJzdWIiOiJhZG1pbiIsImlhdCI6MTYxMjQ4MDk2MiwiZXhwIjoxNjEzMzQ0OTYyfQ.DED0cxCRWXMjkXyaEMW4ztjDJP1J2dkxCOiHktINFoDmnkshzVMcfs2GA3XeyoO30BU6qCjE1hFGh08EwwSiDQ",
    "token_type": "Bearer ",
    "expires_in": 864000000,
    "scope": "read/write"
}

Demo: GET with header =====
protocolV  :HTTP/1.1
Duration   :93ms
Status Code:200
Body       :{"result":{"id":1,"username":"admin","alias":"admin5","status":"Active","imageUrl":null,"authType":null,"roles":["member","admin"],"privileges":[],"membership":null,"authProvider":null,"email":"[email protected]","createdAt":null},"message":null,"error":null}

Demo: GET with retry 3 times =====
protocolV  :HTTP/1.1
Duration   :157ms
Status Code:200
Body       :{"result":{"content":[{"_id":"5fea9d7a5c333a0019a07390","contact":{"option":"Mobile","value":"6135555555"},"departure_area":{"province":"ON","city":"Ottawa"},"arrival_area":{"province":"ON","city":"Toronto(GTA)"},"status":"Active","view":4,"created":"2020-12-29T03:07:38.420Z","modified":"2021-01-02T03:47:25.852Z","poster":{"id":1,"username":"admin","alias":"admin5","status":"Active","imageUrl":null,"authType":null,"roles":null,"privileges":null,"membership":null,"authProvider":null,"email":"[email protected]","createdAt":null},"username":"","departure_date":"2020-12-29T05:00:00.000Z","sub_arrival":"","sub_departure":"","description":"dfg","price":50,"seats":3,"model":"","id":1,"__v":0,"modifiedBy":1},{"_id":"5feff2335c333a0019a0744d","contact":{"option":"Email","value":"[email protected]"},"departure_area":{"province":"ON","city":"Ottawa"},"arrival_area":{"province":"QC","city":"Centre-du-Québec"},"status":"Active","view":0,"created":"2021-01-02T04:10:27.324Z","modified":"2021-01-02T04:11:19.705Z","poster":{"id":1,"username":"admin","alias":"admin5","status":"Active","imageUrl":null,"authType":null,"roles":null,"privileges":null,"membership":null,"authProvider":null,"email":"[email protected]","createdAt":null},"username":"","departure_date":"2021-01-03T05:00:00.000Z","sub_arrival":"outlet","sub_departure":"the bay","description":"123123","price":40,"seats":3,"model":"suv","id":3,"__v":0,"modifiedBy":1},{"_id":"5ff0d456ada7870012e43d55","contact":{"option":"Mobile","value":"1635555555"},"departure_area":{"province":"ON","city":"Ottawa"},"arrival_area":{"province":"ON","city":"Ottawa"},"status":"Active","view":0,"created":"2021-01-02T20:15:18.056Z","modified":"2021-01-02T20:15:18.056Z","poster":{"id":1,"username":"admin","alias":"admin5","status":"Active","imageUrl":null,"authType":null,"roles":null,"privileges":null,"membership":null,"authProvider":null,"email":"[email protected]","createdAt":null},"username":"","departure_date":"2021-01-02T20:14:57.107Z","sub_arrival":"","sub_departure":"","description":"","price":22,"seats":null,"model":"","id":4,"__v":0},{"_id":"5ff858a0ada7870012e43d5f","contact":{"option":"Mobile","value":"6135555555"},"departure_area":{"province":"ON","city":"Ottawa"},"arrival_area":{"province":"ON","city":"Barrie"},"status":"Active","view":0,"created":"2021-01-08T13:05:36.171Z","modified":"2021-01-08T13:05:36.171Z","poster":{"id":8,"username":"long","alias":"long","status":"Active","imageUrl":null,"authType":null,"roles":null,"privileges":null,"membership":null,"authProvider":null,"email":"[email protected]","createdAt":null},"username":"","departure_date":"2021-01-10T05:00:00.000Z","sub_arrival":"mall 2","sub_departure":"mall","description":"test","price":40,"seats":3,"model":"suv","id":5,"__v":0}],"totalElements":4,"limit":10,"totalPages":1,"page":1,"pagingCounter":1,"hasPrevPage":false,"hasNextPage":false,"prevPage":null,"nextPage":null},"success":true,"message":"got rideshare Drivers successfully"}
```




© 2015 - 2025 Weber Informatics LLC | Privacy Policy