Asynchronously make calls to http services. For a discussion of connecting to web services, and for examples of AsyncHttp API methods, refer to Connecting Directly to Web Services with Rhodes.
You can call the AsyncHttp methods from JavaScript as well as Ruby. To use the JavaScript API, add the public/js/rho_javascript_api.js
file – created at build time as part of the application package – to the .html, .erb, or .js file calling the JavaScript method.
You must enable JavaScript by putting rho-javascript
into extensions in your build.yml.
extensions: ["rho-javascript"]
You must have a RhoElements license to use the JavaScript API.
Cancel the current AsyncHttp call.
Ruby syntax:
Rho::AsyncHttp.cancel
JavaScript syntax:
Rho.AsyncHttp.cancel()
Download a file to the specified filename.
Click for an example of download_file.
Ruby syntax:
Rho::AsyncHttp.download_file(:url, :headers, :filename, :callback, :callback_param)
JavaScript syntax:
Rho.AsyncHttp.download_file(url, headers, fileName, callback, callback_param)
:url |
String. url of the request. |
:headers |
Hash of headers to send with the request. |
:filename |
String. The path and name where the downloaded file is received. |
:callback |
Callback function to execute when the request is done. |
:callback_param |
(optional) Hash of parameters to send to the callback. Parameters values should be url encoded, such as
:callback_param => "action=text&title=#{Rho::RhoSupport.url_encode(@params['page_title'])}" .
Values will be decoded automatically in callback.
|
Perform an HTTP GET request to the specified URL.
Ruby syntax:
Rho::AsyncHttp.get(:url, :headers, :callback, :callback_param)
JavaScript syntax:
Rho.AsyncHttp.get(url, headers, callback_function, callback_param)
:url |
URL of the request. |
:headers |
Hash of headers to send with the request. |
:callback |
Callback action to execute when the request is done. |
:callback_param |
(optional) Hash of parameters to send to the callback. Parameters values should be url encoded, such as
:callback_param => "action=text&title=#{Rho::RhoSupport.url_encode(@params['page_title'])}" .
Values will be decoded automatically in callback.
|
:authentication |
(optional) Send Basic Auth header with request. |
:ssl_verify_peer |
(optional) Verify SSL certificates. Default = true. |
The authentication parameter is a hash with parameters such as the following.
Click for an example of setting the authentication parameter in get.
:type |
type of request, such as :basic. |
:username |
the user name. |
:password |
the password. |
Perform HTTP POST request to the specified URL.
Ruby syntax:
Rho::AsyncHttp.post(:url, :headers, :body, :callback, :callback_param)
JavaScript syntax:
Rho.AsyncHttp.post(url, headers, body, callback_function, callback_param)
:url |
URL of the request. |
:headers |
Hash of headers to send with the request. |
:body |
String. The message body of the HTTP POST. |
:callback |
Callback action to execute when the request is done. |
:callback_param |
(optional) Hash of parameters to send to the callback. Parameters values should be url encoded, such as
:callback_param => "action=text&title=#{Rho::RhoSupport.url_encode(@params['page_title'])}" .
Values will be decoded automatically in callback.
|
:authentication |
(optional) Send Basic Auth header with request. |
:ssl_verify_peer |
(optional) Verify SSL certificates. Default = true. |
:http_command |
(optional) HTTP command to send with request: PUT, DELETE, etc. |
The authentication parameter is a hash with parameters such as the following.
Click for an example of setting the authentication parameter in get.
:type |
type of request, such as :basic. |
:username |
the user name. |
:password |
the password. |
Upload the specified file using HTTP POST.
Click for examples of upload_file.
Ruby syntax:
Rho::AsyncHttp.upload_file(:url, :headers, :filename, :body, :callback, :callback_param)
JavaScript syntax:
Rho.AsyncHttp.upload_file(url, headers, fileName, body, callback, callback_param)
:url |
String. URL of the request. |
:headers |
Hash of headers to send with the request. |
:filename |
String. The path and name of the file to be uploaded. |
:body |
Leave this value blank, AsyncHttp will fill in multipart body. |
:callback |
Callback function to execute when the upload request is done. |
:callback_param |
(optional) Hash of parameters to send to the callback. Parameters values should be url encoded, such as
:callback_param => "action=text&title=#{Rho::RhoSupport.url_encode(@params['page_title'])}" .
Values will be decoded automatically in callback.
|
upload_file
can also send multiple files in a single request.
Rho::AsyncHttp.upload_file(:url, :multipart)
:url |
URL of the request. |
:multipart |
Array of hashes containing file information. |
The :multipart
hash contains the following parameters.
:filename |
Name of the file to be uploaded. |
:filename_base |
(optional) Base directory containing the :filename. |
:name |
(optional) File type, defaults to "blob". |
:content_type |
(optional) Content-Type header, defaults to "application/octet-stream". |
The callback for the AsyncHttp methods executes when the AsyncHttp request is completed. The following parameters are available in an AsyncHttp callback.
@params["body"] |
The body of the HTTP response. |
@params["headers"] |
A hash containing the response headers. |
@params["cookies"] |
The server cookies, parsed and usable for subsequent requests. |
@params["http_error"] |
HTTP error code if response code was not 200. |
In the case of a JSON response (Content-Type=“application/json”), the @params["body"]
will be parsed automatically and contain a ruby data structure. Otherwise, @params["body"]
contains the raw response body.
In the case of an XML response (Content-Type=“application/xml”), Rhodes can automatically parse the @params[“body”] as well if you enable the “rexml extension” in your application.