Http2Client
HTTP/2 client over TLS (h2). Maintains a persistent multiplexed connection via a native Http2Session.
client := Http2Client->New("httpbin.org");
resp := client->Get("/get");
if(resp <> Nil) {
resp->GetCode()->PrintLine();
};
client->Close();Operations
- New
- AddHeader
- Close
- Delete
- Get
- IsConnected
- Patch
- Post
- Put
- QuickDelete
- QuickGet
- QuickPatch
- QuickPost
- QuickPut
- RemoveHeader
AddHeader #
Adds a custom request header sent with every subsequent request.
method : public : AddHeader(key:String, value:String) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| key | String | header name (lower-case recommended) |
| value | String | header value |
Example
client := Web.HTTP.Http2Client->New("api.example.com");
client->AddHeader("authorization", "Bearer mytoken");
client->AddHeader("accept", "application/json");
resp := client->Get("/v1/data");
resp->GetCode()->PrintLine();
client->Close();Close #
Closes the HTTP/2 connection and releases resources.
method : public : Close() ~ NilExample
client := Web.HTTP.Http2Client->New("api.example.com");
resp := client->Get("/data");
resp->GetCode()->PrintLine();
client->Close(); # release NGHTTP2 sessionDelete #
Performs an HTTP/2 DELETE request.
method : public : Delete(path:String) ~ ResponseParameters
| Name | Type | Description |
|---|---|---|
| path | String | request path |
Return
| Type | Description |
|---|---|
| Response | Response or Nil on failure |
Example
client := Web.HTTP.Http2Client->New("api.example.com");
resp := client->Delete("/items/42");
resp->GetCode()->PrintLine(); # 204
client->Close();Get #
Performs an HTTP/2 GET request.
method : public : Get(path:String) ~ ResponseParameters
| Name | Type | Description |
|---|---|---|
| path | String | request path (e.g. "/v1/users") |
Return
| Type | Description |
|---|---|
| Response | Response or Nil on failure |
Example
client := Web.HTTP.Http2Client->New("httpbin.org");
resp := client->Get("/get");
if(resp <> Nil) {
resp->GetCode()->PrintLine(); # 200
String->New(resp->GetContent())->PrintLine(); # JSON body
};
client->Close();IsConnected #
Returns true if the connection is established
method : public : IsConnected() ~ BoolExample
client := Web.HTTP.Http2Client->New("api.example.com");
if(client->IsConnected()) {
resp := client->Get("/status");
resp->GetCode()->PrintLine(); # 200
};
client->Close();New # constructor
Creates an HTTP/2 client connected to the given host.
New(host:String, port:Int)Parameters
| Name | Type | Description |
|---|---|---|
| host | String | hostname (e.g. "api.example.com") |
| port | Int | port number (default 443) |
Patch #
Performs an HTTP/2 PATCH request.
method : public : Patch(path:String, body:Byte[], content_type:String) ~ ResponseParameters
| Name | Type | Description |
|---|---|---|
| path | String | request path |
| body | Byte | request body bytes |
| content_type | String | MIME type of the body |
Return
| Type | Description |
|---|---|
| Response | Response or Nil on failure |
Example
client := Web.HTTP.Http2Client->New("api.example.com");
payload := "{\"status\":\"active\"}"->ToByteArray();
resp := client->Patch("/items/1", payload, "application/json");
resp->GetCode()->PrintLine(); # 200
client->Close();Post #
Performs an HTTP/2 POST request.
method : public : Post(path:String, body:Byte[], content_type:String) ~ ResponseParameters
| Name | Type | Description |
|---|---|---|
| path | String | request path |
| body | Byte | request body bytes |
| content_type | String | MIME type of the body |
Return
| Type | Description |
|---|---|
| Response | Response or Nil on failure |
Example
client := Web.HTTP.Http2Client->New("httpbin.org");
payload := "{\"key\":\"value\"}"->ToByteArray();
resp := client->Post("/post", payload, "application/json");
resp->GetCode()->PrintLine(); # 200
client->Close();Put #
Performs an HTTP/2 PUT request.
method : public : Put(path:String, body:Byte[], content_type:String) ~ ResponseParameters
| Name | Type | Description |
|---|---|---|
| path | String | request path |
| body | Byte | request body bytes |
| content_type | String | MIME type of the body |
Return
| Type | Description |
|---|---|
| Response | Response or Nil on failure |
Example
client := Web.HTTP.Http2Client->New("api.example.com");
payload := "{\"name\":\"updated\"}"->ToByteArray();
resp := client->Put("/items/1", payload, "application/json");
resp->GetCode()->PrintLine(); # 200
client->Close();QuickDelete # function
Performs an HTTP/2 DELETE from a URL in one call.
function : QuickDelete(url:Web.HTTP.Url) ~ Web.HTTP.ResponseParameters
| Name | Type | Description |
|---|---|---|
| url | Url | URL to delete |
Return
| Type | Description |
|---|---|
| Response | Response or Nil on failure |
Example
url := Web.HTTP.Url->New("https://api.example.com/items/7");
resp := Web.HTTP.Http2Client->QuickDelete(url);
resp->GetCode()->PrintLine(); # 204QuickDelete # function
Performs an HTTP/2 DELETE from a URL with custom headers.
function : QuickDelete(url:Web.HTTP.Url, headers:Map<String,String>) ~ Web.HTTP.ResponseParameters
| Name | Type | Description |
|---|---|---|
| url | Url | URL to delete |
| headers | Map<String,String> | optional key/value headers |
Return
| Type | Description |
|---|---|
| Response | Response or Nil on failure |
QuickGet # function
Performs an HTTP/2 GET from a URL in one call.
function : QuickGet(url:Web.HTTP.Url) ~ Web.HTTP.ResponseParameters
| Name | Type | Description |
|---|---|---|
| url | Url | URL to fetch |
Return
| Type | Description |
|---|---|
| Response | Response or Nil on failure |
Example
url := Web.HTTP.Url->New("https://httpbin.org/get");
resp := Web.HTTP.Http2Client->QuickGet(url);
if(resp <> Nil) {
resp->GetCode()->PrintLine(); # 200
String->New(resp->GetContent())->PrintLine();
};QuickGet # function
Performs an HTTP/2 GET from a URL with custom headers.
function : QuickGet(url:Web.HTTP.Url, headers:Map<String,String>) ~ Web.HTTP.ResponseParameters
| Name | Type | Description |
|---|---|---|
| url | Url | URL to fetch |
| headers | Map<String,String> | optional key/value headers |
Return
| Type | Description |
|---|---|
| Response | Response or Nil on failure |
QuickPatch # function
Performs an HTTP/2 PATCH from a URL in one call.
function : QuickPatch(url:Web.HTTP.Url, data:Byte[]) ~ Web.HTTP.ResponseParameters
| Name | Type | Description |
|---|---|---|
| url | Url | URL to patch |
| data | Byte | request body |
Return
| Type | Description |
|---|---|
| Response | Response or Nil on failure |
QuickPatch # function
Performs an HTTP/2 PATCH from a URL with a content type.
function : QuickPatch(url:Web.HTTP.Url, data:Byte[], content_type:String) ~ Web.HTTP.ResponseParameters
| Name | Type | Description |
|---|---|---|
| url | Url | URL to patch |
| data | Byte | request body |
| content_type | String | MIME type |
Return
| Type | Description |
|---|---|
| Response | Response or Nil on failure |
QuickPatch # function
Performs an HTTP/2 PATCH from a URL with content type and headers.
function : QuickPatch(url:Web.HTTP.Url, data:Byte[], content_type:String, headers:Map<String,String>) ~ Web.HTTP.ResponseParameters
| Name | Type | Description |
|---|---|---|
| url | Url | URL to patch |
| data | Byte | request body |
| content_type | String | MIME type |
| headers | Map<String,String> | optional key/value headers |
Return
| Type | Description |
|---|---|
| Response | Response or Nil on failure |
QuickPost # function
Performs an HTTP/2 POST from a URL in one call.
function : QuickPost(url:Web.HTTP.Url, data:Byte[]) ~ Web.HTTP.ResponseParameters
| Name | Type | Description |
|---|---|---|
| url | Url | URL to post to |
| data | Byte | request body |
Return
| Type | Description |
|---|---|
| Response | Response or Nil on failure |
Example
url := Web.HTTP.Url->New("https://httpbin.org/post");
body := "{\"x\":1}"->ToByteArray();
resp := Web.HTTP.Http2Client->QuickPost(url, body, "application/json");
resp->GetCode()->PrintLine(); # 200QuickPost # function
Performs an HTTP/2 POST from a URL with a content type.
function : QuickPost(url:Web.HTTP.Url, data:Byte[], content_type:String) ~ Web.HTTP.ResponseParameters
| Name | Type | Description |
|---|---|---|
| url | Url | URL to post to |
| data | Byte | request body |
| content_type | String | MIME type |
Return
| Type | Description |
|---|---|
| Response | Response or Nil on failure |
QuickPost # function
Performs an HTTP/2 POST from a URL with content type and headers.
function : QuickPost(url:Web.HTTP.Url, data:Byte[], content_type:String, headers:Map<String,String>) ~ Web.HTTP.ResponseParameters
| Name | Type | Description |
|---|---|---|
| url | Url | URL to post to |
| data | Byte | request body |
| content_type | String | MIME type |
| headers | Map<String,String> | optional key/value headers |
Return
| Type | Description |
|---|---|
| Response | Response or Nil on failure |
QuickPut # function
Performs an HTTP/2 PUT from a URL in one call.
function : QuickPut(url:Web.HTTP.Url, data:Byte[]) ~ Web.HTTP.ResponseParameters
| Name | Type | Description |
|---|---|---|
| url | Url | URL to put to |
| data | Byte | request body |
Return
| Type | Description |
|---|---|
| Response | Response or Nil on failure |
QuickPut # function
Performs an HTTP/2 PUT from a URL with a content type.
function : QuickPut(url:Web.HTTP.Url, data:Byte[], content_type:String) ~ Web.HTTP.ResponseParameters
| Name | Type | Description |
|---|---|---|
| url | Url | URL to put to |
| data | Byte | request body |
| content_type | String | MIME type |
Return
| Type | Description |
|---|---|
| Response | Response or Nil on failure |
QuickPut # function
Performs an HTTP/2 PUT from a URL with content type and headers.
function : QuickPut(url:Web.HTTP.Url, data:Byte[], content_type:String, headers:Map<String,String>) ~ Web.HTTP.ResponseParameters
| Name | Type | Description |
|---|---|---|
| url | Url | URL to put to |
| data | Byte | request body |
| content_type | String | MIME type |
| headers | Map<String,String> | optional key/value headers |
Return
| Type | Description |
|---|---|
| Response | Response or Nil on failure |
RemoveHeader #
Removes a previously added request header.
method : public : RemoveHeader(key:String) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| key | String | header name |
Example
client := Web.HTTP.Http2Client->New("api.example.com");
client->AddHeader("x-trace-id", "abc");
client->RemoveHeader("x-trace-id"); # strip before next request
resp := client->Get("/health");
client->Close();