Class HttpRequestBuilder

java.lang.Object
org.openhab.core.io.net.http.HttpRequestBuilder

@NonNullByDefault public class HttpRequestBuilder extends Object
Builder class to construct http requests
Author:
Martin van Wingerden - Initial contribution
  • Method Details

    • getFrom

      public static HttpRequestBuilder getFrom(String url)
      Construct an http request builder to get data from an url
      Parameters:
      url - to fetch the data from
      Returns:
      a request builder to construct and complete the request
    • postTo

      public static HttpRequestBuilder postTo(String url)
      Construct an http request builder to post data to a url
      Parameters:
      url - to post data to
      Returns:
      a request builder to construct and complete the request
    • withTimeout

      public HttpRequestBuilder withTimeout(Duration timeout)
      Add a timeout for this request
      Parameters:
      timeout - the timeout for this http request as a Duration
      Returns:
      a request builder to construct and complete the request
    • withHeader

      public HttpRequestBuilder withHeader(String header, String value)
      Add an additional header to the request
      Parameters:
      header - name of the header, eg. Content-Type
      value - value of the header, eg. "application/json"
      Returns:
      a request builder to construct and complete the request
    • withContent

      public HttpRequestBuilder withContent(String content)
      Add content to this request
      Parameters:
      content - a string containing the data to be pushed to the url
      Returns:
      a request builder to construct and complete the request
    • withContent

      public HttpRequestBuilder withContent(String content, String contentType)
      Add content with a specific type to this request
      Parameters:
      content - a string containing the data to be pushed to the url
      contentType - the content type of the given content
      Returns:
      a request builder to construct and complete the request
    • getContentAsString

      public String getContentAsString() throws IOException
      Executes the build request
      Returns:
      the response body or null when the request went wrong
      Throws:
      IOException - when the request execution failed, timed out or it was interrupted