Coder Social home page Coder Social logo

rest-client's Introduction

Http Rest Client

Join the chat at https://gitter.im/m-szalik/rest-client Build Status

RestClient that parses:

Features

  • Support for cookies
  • Flexibility provided by plugins

Maven artifact

<dependency>
    <groupId>org.jsoftware</groupId>
    <artifactId>rest-client</artifactId>
    <version>1.2</version>
</dependency>

Examples:

Http GET request for JSON response:

RestClient restClient = new DefaultRestClient();                             // new instance of RestClient
restClient.setPlugins(Arrays.asList(new VerbosePlugin(false, System.out)));  // add verbose plugin - it prints request and response to stdout
RestClientResponse response = restClient.get("https://api.stackexchange.com/2.2/answers") // API URL (http get)
                                .parameter("pagesize", 5)                         // Parameters
                                .parameter("order", "desc")
                                .parameter("sort", "activity")
                                .parameter("site", "stackoverflow")
                                .execute();                                         // execute http call

List<Number> questionIds = (List<Number>) response.json("$..answer_id");            // get all answer_id fields from response

More about syntax for .json(String) method argument can be found here.

Http GET request for XML response:

RestClient restClient = new DefaultRestClient();                             // new instance of RestClient
RestClientResponse response = restClient.get("https://api.somewhere.com.com/xml/call") // API URL (http get)
                                .execute();
NodeList nList = (NodeList) response.xPath("/answer/items/", XPathConstants.NODESET);  // fetch xml data using XPath notation

Http POST request:

Emulate sending html form:

RestClient restClient = new DefaultRestClient();                             // new instance of RestClient
RestClientResponse response = restClient.post("https://somewhere.com")       // API URL (http post)
                                .parameter("username", "John")               // Http form parameters
                                .parameter("password", "John's password")
                                .execute();                                  // execute http call

Send JSON:

RestClient restClient = new DefaultRestClient();                             // new instance of RestClient
RestClientResponse response = restClient.post("https://somewhere.com")       // API URL (http post)
                                .body("{\"username\" : \"John\"}", org.apache.http.entity.ContentType.APPLICATION_JSON)      // request body
                                .execute();                                  // execute http call

Add custom header

RestClient restClient = new DefaultRestClient();
    RestClientResponse response = restClient.get("https://somewhere.com")
                                    .header("headerName", "headerValue")    // add http header
                                    .execute();                             // execute http call

rest-client's People

Contributors

m-szalik avatar gitter-badger avatar

Watchers

John Byrne 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.