public final class HttpClientMethods extends Object
Provides one-liner methods for HTTP related operations.
HTTP (Hypertext Transfer Protocol) is the set of rules for transferring files on the World Wide Web. The one-liners in this class make it easy to execute HTTP requests (GET/POST), and read responses by returning them as strings.
Constructor and Description |
---|
HttpClientMethods(io.toro.martini.http.ESBHttpClient esbHttpClient) |
Modifier and Type | Method and Description |
---|---|
static String |
http(String uri)
Send an HTTP GET request.
|
static String |
http(String uri,
groovy.lang.Closure<String> body)
Send an HTTP POST request.
|
static String |
http(String uri,
Map params)
Send an HTTP request.
|
static String |
http(String uri,
Map params,
Map headers)
Send an HTTP request.
|
static String |
http(String uri,
Map params,
Map headers,
byte[] body)
Send an HTTP request.
|
static String |
http(String uri,
Map params,
Map headers,
groovy.lang.Closure<String> body)
Send an HTTP request.
|
static String |
http(String uri,
Map params,
Map headers,
String body)
Send an HTTP request.
|
static String |
http(String uri,
String body)
Send an HTTP POST request.
|
@Autowired HttpClientMethods(io.toro.martini.http.ESBHttpClient esbHttpClient)
public static String http(String uri) throws ToroException
Send an HTTP GET request.
Example usage:
def response = 'http://wsf.cdyne.com/WeatherWS/Weather.asmx'.http()
uri
- the URI where the request will be sentToroException
public static String http(String uri, String body) throws ToroException
Send an HTTP POST request.
Example usage:
def response = 'http://www.example.com'.http('Hello, world ')
uri
- the URI where the request will be sentbody
- the body content to be sent with the request as a stringToroException
public static String http(String uri, groovy.lang.Closure<String> body) throws ToroException
Send an HTTP POST request.
Example usage:
boolean success = false; def response = "http://www.example.com".http() { if (success) "Successful" // returns the string even without the keyword return else "Failed to authenticate" }
uri
- the URI where the request will be sentbody
- a closure returning a string which will be used as the body content of the requestToroException
public static String http(String uri, Map params) throws ToroException
Send an HTTP request.
Example usage:
def properties = [ method: "POST", email: "user@your.org", password: "superS3CR3Tpa$$word" ] def response = "https://www.example.com/login".http( properties )
uri
- the URI where the request will be sentparams
- optional parameters to be sent as part of the requestToroException
public static String http(String uri, Map params, Map headers) throws ToroException
Send an HTTP request.
Example usage:
def properties = [ method: "POST", email: "user@your.org", password: "superS3CR3Tpa$$word" ] def header = [ "Content-Type": "text/xml", "Connection": "keep-alive" ] def response = "https://www.example.com/login".http( properties, header )
uri
- the URI where the request will be sentparams
- optional parameters to be sent as part of the requestheaders
- optional headers to be sent as part of the requestToroException
public static String http(String uri, Map params, Map headers, String body) throws ToroException
Send an HTTP request.
Example usage:
def properties = [ method: "POST", email: "user@your.org", password: "superS3CR3Tpa$$word" ] def header = [ "Content-Type": "text/xml", "Connection": "keep-alive" ] def response = "https://www.example.com/login".http( properties, header, "Hello, world " )
uri
- the URI where the request will be sentparams
- optional parameters to be sent as part of the requestheaders
- optional headers to be sent as part of the requestbody
- the body content to be sent with the request as a stringToroException
public static String http(String uri, Map params, Map headers, groovy.lang.Closure<String> body) throws ToroException
Send an HTTP request.
Example usage:
def properties = [ method: "POST", email: "user@your.org", password: "superS3CR3Tpa$$word" ] def header = [ "Content-Type": "text/xml", "Connection": "keep-alive" ] boolean success = header != null def response = "https://www.example.com/login".http( properties, header, { if ( success ) "Successful" // returns the string even without the keyword return else "Failed to authenticate" })
uri
- the URI where the request will be sentbody
- a closure returning a string which will be used as the body content of the requestparams
- optional parameters to be sent as part of the requestheaders
- optional headers to be sent as part of the requestToroException
public static String http(String uri, Map params, Map headers, byte[] body) throws ToroException
Send an HTTP request.
Example usage:
def properties = [ method: "POST", email: "user@your.org", password: "superS3CR3Tpa$$word" ] def header = [ "Content-Type": "text/xml", "Connection": "keep-alive" ] def body = "The quick fox jumped over the lazy dog".getBytes() def response = "https://www.example.com/login".http( properties, header, body )
uri
- the URI where the request will be sentbody
- the body content to be sent with the request as an array of bytesparams
- optional parameters to be sent as part of the requestheaders
- optional headers to be sent as part of the requestToroException
Copyright © 2020. All rights reserved.