Class: OpenHAB::Core::Actions::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/openhab/core/actions/http.rb

Overview

The HTTP actions allow you to send HTTP requests and receive the response.

Examples:

# Send a GET request
headers = {
  "User-Agent": "JRuby/1.2.3", # enclose in quotes if the key contains dashes
  Accept: "application/json",
}
response = HTTP.get("http://example.com/list", headers: headers)

See Also:

Class Method Summary collapse

Class Method Details

.send_http_delete_request(url, headers: {}, timeout: nil) ⇒ String? Also known as: delete

Sends an HTTP DELETE request and returns the result as a String.

Parameters:

  • url (String)
  • headers (Hash<String, String>, Hash<Symbol, String>) (defaults to: {})

    A hash of headers to send with the request. Keys are strings or symbols, values are strings. Underscores in symbolic keys are replaced with dashes.

  • timeout (Duration, int, nil) (defaults to: nil)

    Timeout (in milliseconds, if given as an Integer)

Returns:

  • (String)

    the response body

  • (nil)

    if an error occurred



99
100
101
# File 'lib/openhab/core/actions/http.rb', line 99

def send_http_delete_request(url, headers: {}, timeout: nil)
  request(:sendHttpDeleteRequest, url, headers:, timeout:)
end

.send_http_get_request(url, headers: {}, timeout: nil) ⇒ String? Also known as: get

Sends an HTTP GET request and returns the result as a String.

Parameters:

  • url (String)
  • headers (Hash<String, String>, Hash<Symbol, String>) (defaults to: {})

    A hash of headers to send with the request. Symbolic keys will be converted to strings.

  • timeout (Duration, int, nil) (defaults to: nil)

    Timeout (in milliseconds, if given as an Integer)

Returns:

  • (String)

    the response body

  • (nil)

    if an error occurred



30
31
32
# File 'lib/openhab/core/actions/http.rb', line 30

def send_http_get_request(url, headers: {}, timeout: nil)
  request(:sendHttpGetRequest, url, headers:, timeout:, default_timeout: 5_000)
end

.send_http_patch_request(url, content_type = nil, content = nil, headers: {}, timeout: nil) ⇒ String? Also known as: patch

Sends an HTTP PATCH request and returns the result as a String.

Parameters:

  • url (String)
  • content_type (String) (defaults to: nil)
  • content (String) (defaults to: nil)
  • headers (Hash<String, String>, Hash<Symbol, String>) (defaults to: {})

    A hash of headers to send with the request. Symbolic keys will be converted to strings.

  • timeout (Duration, int, nil) (defaults to: nil)

    Timeout (in milliseconds, if given as an Integer)

Returns:

  • (String)

    the response body

  • (nil)

    if an error occurred

Since:

  • openHAB 5.3



83
84
85
# File 'lib/openhab/core/actions/http.rb', line 83

def send_http_patch_request(url, content_type = nil, content = nil, headers: {}, timeout: nil)
  request(:sendHttpPatchRequest, url, content_type, content, headers:, timeout:)
end

.send_http_post_request(url, content_type = nil, content = nil, headers: {}, timeout: nil) ⇒ String? Also known as: post

Sends an HTTP POST request and returns the result as a String.

Parameters:

  • url (String)
  • content_type (String) (defaults to: nil)
  • content (String) (defaults to: nil)
  • headers (Hash<String, String>, Hash<Symbol, String>) (defaults to: {})

    A hash of headers to send with the request. Symbolic keys will be converted to strings.

  • timeout (Duration, int, nil) (defaults to: nil)

    Timeout (in milliseconds, if given as an Integer)

Returns:

  • (String)

    the response body

  • (nil)

    if an error occurred



64
65
66
# File 'lib/openhab/core/actions/http.rb', line 64

def send_http_post_request(url, content_type = nil, content = nil, headers: {}, timeout: nil)
  request(:sendHttpPostRequest, url, content_type, content, headers:, timeout:)
end

.send_http_put_request(url, content_type = nil, content = nil, headers: {}, timeout: nil) ⇒ String? Also known as: put

Sends an HTTP PUT request and returns the result as a String.

Parameters:

  • url (String)
  • content_type (String) (defaults to: nil)
  • content (String) (defaults to: nil)
  • headers (Hash<String, String>, Hash<Symbol, String>) (defaults to: {})

    A hash of headers to send with the request. Symbolic keys will be converted to strings.

  • timeout (Duration, int, nil) (defaults to: nil)

    Timeout (in milliseconds, if given as an Integer)

Returns:

  • (String)

    the response body

  • (nil)

    if an error occurred



47
48
49
# File 'lib/openhab/core/actions/http.rb', line 47

def send_http_put_request(url, content_type = nil, content = nil, headers: {}, timeout: nil)
  request(:sendHttpPutRequest, url, content_type, content, headers:, timeout:)
end