org.jsoftware:rest-client

Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/

License

License

Categories

Categories

CLI User Interface
GroupId

GroupId

org.jsoftware
ArtifactId

ArtifactId

rest-client
Last Version

Last Version

1.5
Release Date

Release Date

Type

Type

jar
Description

Description

Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/
Project Organization

Project Organization

jsoftware.org
Source Code Management

Source Code Management

https://github.com/m-szalik/rest-client.git

Download rest-client

How to add to project

<!-- https://jarcasting.com/artifacts/org.jsoftware/rest-client/ -->
<dependency>
    <groupId>org.jsoftware</groupId>
    <artifactId>rest-client</artifactId>
    <version>1.5</version>
</dependency>
// https://jarcasting.com/artifacts/org.jsoftware/rest-client/
implementation 'org.jsoftware:rest-client:1.5'
// https://jarcasting.com/artifacts/org.jsoftware/rest-client/
implementation ("org.jsoftware:rest-client:1.5")
'org.jsoftware:rest-client:jar:1.5'
<dependency org="org.jsoftware" name="rest-client" rev="1.5">
  <artifact name="rest-client" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.jsoftware', module='rest-client', version='1.5')
)
libraryDependencies += "org.jsoftware" % "rest-client" % "1.5"
[org.jsoftware/rest-client "1.5"]

Dependencies

compile (6)

Group / Artifact Type Version
commons-io : commons-io jar 2.5
org.apache.httpcomponents : httpclient jar 4.5.2
org.apache.commons : commons-lang3 jar 3.4
com.jayway.jsonpath : json-path jar 2.2.0
org.jsoup : jsoup jar 1.9.2
org.jsoftware : jsoftware-utils jar 1.0

provided (1)

Group / Artifact Type Version
org.jetbrains : annotations jar 15.0

test (1)

Group / Artifact Type Version
junit : junit jar [4.12,)

Project Modules

There are no modules declared in this project.

Http Rest Client

Join the chat at https://gitter.im/m-szalik/rest-client Build Status codecov.io Dependency Status Codacy Badge

RestClient that parses:

Features

  • Support for cookies
  • Flexibility provided by plugins

Requirements

  • Java 8 or newer

Maven artifact

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

Examples:

Http GET request for JSON response:

RestClientFactory factory = new RestClientFactory();
try(RestClient restClient = factory.newRestClient()) {
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:

RestClientFactory factory = new RestClientFactory();
try(RestClient restClient = factory.newRestClient()) {
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:

RestClientFactory factory = new RestClientFactory();
try(RestClient restClient = factory.newRestClient()) {                       // 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:

RestClientFactory factory = new RestClientFactory();
try(RestClient restClient = factory.newRestClient()) {                       // 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

RestClientFactory factory = new RestClientFactory();
try(RestClient restClient = factory.newRestClient()) {
RestClientResponse response = restClient.get("https://somewhere.com")
                                    .header("headerName", "headerValue")    // add http header
                                    .execute();                             // execute http call
}

License

Apache License 2.0

Versions

Version
1.5
1.4
1.3
1.2