Class HttpClientMethods


  • 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 Detail

      • HttpClientMethods

        @Autowired
        HttpClientMethods​(io.toro.martini.http.ESBHttpClient esbHttpClient)
    • Method Detail

      • http

        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()
         

        Parameters:
        uri - the URI where the request will be sent
        Returns:
        the response message entity content as a string
        Throws:
        ToroException
        Since:
        1.0
      • http

        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')
         

        Parameters:
        uri - the URI where the request will be sent
        body - the body content to be sent with the request as a string
        Returns:
        the response message entity content as a string
        Throws:
        ToroException
        Since:
        1.0
      • http

        public static String http​(String uri,
                                  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"
         }
         
        Parameters:
        uri - the URI where the request will be sent
        body - a closure returning a string which will be used as the body content of the request
        Returns:
        the response message entity content as a string
        Throws:
        ToroException
        Since:
        1.0
      • http

        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 )
         
        Parameters:
        uri - the URI where the request will be sent
        params - optional parameters to be sent as part of the request
        Returns:
        the response message entity content as a string
        Throws:
        ToroException
        Since:
        1.0
      • http

        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 )
         
        Parameters:
        uri - the URI where the request will be sent
        params - optional parameters to be sent as part of the request
        headers - optional headers to be sent as part of the request
        Returns:
        the response message entity content as a string
        Throws:
        ToroException
        Since:
        1.0
      • http

        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" )
         
        Parameters:
        uri - the URI where the request will be sent
        params - optional parameters to be sent as part of the request
        headers - optional headers to be sent as part of the request
        body - the body content to be sent with the request as a string
        Returns:
        the response message entity content as a string
        Throws:
        ToroException
        Since:
        1.0
      • http

        public static String http​(String uri,
                                  Map params,
                                  Map headers,
                                  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"
         })
         
        Parameters:
        uri - the URI where the request will be sent
        body - a closure returning a string which will be used as the body content of the request
        params - optional parameters to be sent as part of the request
        headers - optional headers to be sent as part of the request
        Returns:
        the response message entity content as a string
        Throws:
        ToroException
        Since:
        1.0
      • http

        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 )
         
        Parameters:
        uri - the URI where the request will be sent
        body - the body content to be sent with the request as an array of bytes
        params - optional parameters to be sent as part of the request
        headers - optional headers to be sent as part of the request
        Returns:
        the response message entity content as a string
        Throws:
        ToroException
        Since:
        1.0