hs.http
ModuleHTTP client module for making network requests from JavaScript.
All request methods return Promises that resolve with a result object containing
status (number), body (string), and headers (object). On network failure,
status is -1 and body is an empty string.
Quick start
hs.http.get("https://api.example.com/data").then(r => {
if (r.status === 200) {
console.log("Got: " + r.body)
}
})
Types
This module provides the following types:
Properties
This module has no properties.
Methods
hs.http.get(url, headers) -> Promise<{status: number, url: string, body: string, headers: object}>
Perform an HTTP GET request.
hs.http.get(url, headers) -> Promise<{status: number, url: string, body: string, headers: object}>
| Name | Type | Description |
|---|---|---|
| url | string | The URL to request. |
| headers | [String: String] | Optional dictionary of request headers. |
hs.http.get("https://httpbin.org/get").then(r => console.log(r.status))
hs.http.post(url, body, headers) -> Promise<{status: number, url: string, body: string, headers: object}>
Perform an HTTP POST request.
hs.http.post(url, body, headers) -> Promise<{status: number, url: string, body: string, headers: object}>
| Name | Type | Description |
|---|---|---|
| url | string | The URL to request. |
| body | string | Optional request body string. |
| headers | [String: String] | Optional dictionary of request headers. |
hs.http.post("https://httpbin.org/post", '{"key":"val"}', {"Content-Type": "application/json"}).then(r => console.log(r.status))
hs.http.put(url, body, headers) -> Promise<{status: number, url: string, body: string, headers: object}>
Perform an HTTP PUT request.
hs.http.put(url, body, headers) -> Promise<{status: number, url: string, body: string, headers: object}>
| Name | Type | Description |
|---|---|---|
| url | string | The URL to request. |
| body | string | Optional request body string. |
| headers | [String: String] | Optional dictionary of request headers. |
hs.http.put("https://httpbin.org/put", "updated data", null).then(r => console.log(r.status))
hs.http.doRequest(url, method, body, headers) -> Promise<{status: number, url: string, body: string, headers: object}>
Perform an HTTP request with any method (GET, POST, PUT, DELETE, PATCH, etc.).
Use this for methods not covered by the convenience helpers, such as DELETE or PATCH.
hs.http.doRequest(url, method, body, headers) -> Promise<{status: number, url: string, body: string, headers: object}>
| Name | Type | Description |
|---|---|---|
| url | string | The URL to request. |
| method | string | The HTTP method string (e.g. "DELETE", "PATCH", "HEAD"). |
| body | string | Optional request body string. |
| headers | [String: String] | Optional dictionary of request headers. |
hs.http.doRequest("https://httpbin.org/delete", "DELETE", null, null).then(r => console.log(r.status))
hs.http.encodeForQuery(string) -> string
URL-encode a string for use as a query parameter value.
Encodes characters that are illegal in a URL query string (including ?, =, +, &, #)
using percent-encoding.
hs.http.encodeForQuery(string) -> string
| Name | Type | Description |
|---|---|---|
| string | string | The string to encode. |
console.log(hs.http.encodeForQuery("hello world & more=stuff"))
// "hello%20world%20%26%20more%3Dstuff"
hs.http.urlParts(url) -> [String: Any]
Parse a URL into its component parts.
Returns an object containing only the fields present in the URL. The queryItems field
is an array of {name, value} objects from the query string.
hs.http.urlParts(url) -> [String: Any]
| Name | Type | Description |
|---|---|---|
| url | string | The URL string to parse. |
scheme, host, port, user, password, path, query, fragment, queryItems. Returns null if the URL is unparseable.const parts = hs.http.urlParts("https://user:pass@example.com:8080/path?key=val#frag")
console.log(parts.host) // "example.com"
console.log(parts.port) // 8080
hs.http.convertHtmlEntities(string) -> string
Convert HTML entities in a string to their UTF-8 character equivalents.
Handles named entities (e.g. &, <, ©), decimal numeric references
(&), and hexadecimal numeric references (&).
hs.http.convertHtmlEntities(string) -> string
| Name | Type | Description |
|---|---|---|
| string | string | The string containing HTML entities. |
console.log(hs.http.convertHtmlEntities("<div>Hello & World</div>"))
// "<div>Hello & World</div>"
hs.http.openWebSocket(url) -> HSWebSocket
Open a WebSocket connection to the given URL.
The connection begins immediately. Use the returned object's chainable setter methods to
register event callbacks. The connection stays open, with its callbacks, until either end
closes it, even if you don't keep a reference to it. It is automatically closed when
hs.reload() is called or the engine shuts down.
hs.http.openWebSocket(url) -> HSWebSocket
| Name | Type | Description |
|---|---|---|
| url | string | The WebSocket URL (ws:// or wss://). |
HSWebSocket object, or null if the URL is invalid.const ws = hs.http.openWebSocket("ws://localhost:8080/ws")
.setOpenCallback(() => ws.send("Hello!"))
.setMessageCallback(msg => console.log("Got: " + msg))
.setCloseCallback((code, reason) => console.log("Closed: " + code))