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<object>
Perform an HTTP GET request.
hs.http.get(url, headers) -> Promise<{status: number, url: string, body: string, headers: Record<string, string>}>
| Name | Type | Description |
|---|---|---|
| url | string | The URL to request. |
| headers optional | [String: String] | Optional dictionary of request headers. |
| Field | Type | Description |
|---|---|---|
| status | number | |
| url | string | |
| body | string | |
| headers | Record<string, string> |
hs.http.get("https://httpbin.org/get").then(r => console.log(r.status))
hs.http.post(url, body, headers) -> Promise<object>
Perform an HTTP POST request.
hs.http.post(url, body, headers) -> Promise<{status: number, url: string, body: string, headers: Record<string, string>}>
| Name | Type | Description |
|---|---|---|
| url | string | The URL to request. |
| body optional | string | Optional request body string. |
| headers optional | [String: String] | Optional dictionary of request headers. |
| Field | Type | Description |
|---|---|---|
| status | number | |
| url | string | |
| body | string | |
| headers | Record<string, string> |
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<object>
Perform an HTTP PUT request.
hs.http.put(url, body, headers) -> Promise<{status: number, url: string, body: string, headers: Record<string, string>}>
| Name | Type | Description |
|---|---|---|
| url | string | The URL to request. |
| body optional | string | Optional request body string. |
| headers optional | [String: String] | Optional dictionary of request headers. |
| Field | Type | Description |
|---|---|---|
| status | number | |
| url | string | |
| body | string | |
| headers | Record<string, string> |
hs.http.put("https://httpbin.org/put", "updated data", null).then(r => console.log(r.status))
hs.http.doRequest(url, method, body, headers) -> Promise<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: Record<string, string>}>
| Name | Type | Description |
|---|---|---|
| url | string | The URL to request. |
| method | string | The HTTP method string (e.g. "DELETE", "PATCH", "HEAD"). |
| body optional | string | Optional request body string. |
| headers optional | [String: String] | Optional dictionary of request headers. |
| Field | Type | Description |
|---|---|---|
| status | number | |
| url | string | |
| body | string | |
| headers | Record<string, string> |
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) -> object | null
Parse a URL into its component parts.
hs.http.urlParts(url) -> object | null
| Name | Type | Description |
|---|---|---|
| url | string | The URL string to parse. |
null if the URL is unparseable.| Field | Type | Description |
|---|---|---|
| scheme optional | string | e.g. "https" |
| host optional | string | |
| port optional | number | |
| user optional | string | |
| password optional | string | |
| path | string | The path, which may be empty |
| query optional | string | The query string, without the ? |
| fragment optional | string | The fragment, without the # |
| queryItems optional | object[] | The items of the query string |
| name | string | |
| value | string |
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))