Giter Club home page Giter Club logo

jrequests's Introduction

Java Requests

Easy to use java request library. This was earlier part of https://github.com/sapandang/Load-Director

Design

Aim is to provide simple syntax for sending requests. For this all the methods are set as multi parameters.

Add depedency

Select the version from the repo

implementation 'io.github.sapandang:Jrequests:1.0'
  • Maven
<dependency>
    <groupId>io.github.sapandang</groupId>
    <artifactId>Jrequests</artifactId>
    <version>1.0</version>
</dependency>

Usage

Get

String BASE_URL="https://webhook.site/";

Requests requests = new Requests();
String url = BASE_URL+"/getTest";

Headers headers = new Headers();
headers.put("Content-Type","application/json");

ResponseData responseData =  requests.get(url,headers);
String resbody = responseData.body;
System.out.println(":GET: "+resbody);

POST

Requests requests = new Requests();
String url = BASE_URL+"/PostTest";
Headers headers = new Headers();
headers.put("Content-Type","application/json");
ResponseData responseData =  requests.postRaw(url,"this is the body to post",headers);
String resbody = responseData.body;
System.out.println(":POST: "+resbody);

Multipart form body

Requests requests = new Requests();
Headers headers = new Headers();
headers.put("Content-Type","application/json");
String authUrl = BASE_URL+"/app/authentication";
MultiPartFormBody authData = new MultiPartFormBody();
authData.add("j_username", "myuser")
        .add("j_password", "5dfae4ace9e99af5235b184086c78c02b526d57ed7d5f13b34d71d4884292fea")
        .add("remember-me", "false")
        .add("submit", "Login");
ResponseData responseData =  requests.postFormData(authUrl,authData,headers);
System.out.println(":MULTIPART: "+responseData.body);

Post File

Requests requests = new Requests();
String url = BASE_URL+"/PostFile";
ResponseData fileRepose = requests.postfile(BASE_URL+"/postFile",
        "ThisIsFileKey",
        "File Name",
        new File("myfile"));

//send with media type
ResponseData fileRepose2 = requests.postfile(BASE_URL+"/postFile2",
        "application/zip",
        "ThisIsFileKey",
        "File Name",
        new File("a.zip"));

Async Requests

Just add pass the call back to any method to make it async

new AsyncResponseCallback() {
    @Override
    public void onResponse(ResponseData response) {

        System.out.println(":POST ASYNC: ");
    }
}

example:

Requests requests = new Requests();
String url = BASE_URL+"/PostAsycTest";
Headers headers = new Headers();
headers.put("Content-Type","application/json");
requests.postRaw(url, "this is the body to post", headers, new AsyncResponseCallback() {
    @Override
    public void onResponse(ResponseData response) {

        System.out.println(":POST ASYNC: ");
    }
});
System.out.println("ASYNC REQUEST SEND");

jrequests's People

Contributors

sapandang avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.