k.net.url
Easy synchronous HTTP requests (string / object / binary / full response)
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
| KScript API, Page / View server scripts | Outbound HTTP access; use httpClient for complex proxying or batch tasks | One-line fetching of remote text, JSON, binary images, or simple form/JSON posts |
k.net.url initiates synchronous HTTP calls to remote URLs and returns a string, object, number[] (byte array), or CurlResponse depending on the method. It is concise and well-suited for quick fetches or form/JSON posts in scripts.
Quick Start
k.api.get("fetchJson", () => {
const data = k.net.url.getJson("https://httpbin.org/json")
const bytes = k.net.url.getAsBinary("https://www.kooboo.com/img/logo-white.png")
return {
hasSlides: data && !!data.slideshow,
imageBytes: bytes ? bytes.length : 0
}
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
get / getAsObject / getJson / getAsBinary | Synchronous GET as string, object, JSON, or bytes | string / any / number[] / CurlResponse |
post / postAsObject / postAsBinary / postData / postDataAsObject | Synchronous POST with string or JSON body | string / any / number[] / CurlResponse |
postform(url, data, ...) | Submit application/x-www-form-urlencoded form data | string / CurlResponse |
put / putAsObject / patch / patchAsObject / patchData / delete / deleteAsObject | Synchronous PUT, PATCH, and DELETE requests | string / any / CurlResponse |
sendMessage(requestMessage, data) | Send a request configured by data (url, method, optional params/headers/body/multipartpartkey), optionally forwarding multipart form/files via requestMessage (k.request) | CurlResponse |
downloadZip(url) | Fetch remote ZIP bytes and write directly to the HTTP response | void |
When to use httpClient vs url
- Proxy forwarding, complex headers, multipart uploads, custom timeouts,
KHttpResponsefile saving, or batch tasks → use k.net.httpClient. - Quickly fetch remote image or file bytes →
k.net.url.getAsBinary(url). - Inspect status codes/headers or send cookies → use the
cookiesoverloads onget/post/put/delete/patch/postform, orsendMessage(), which all returnCurlResponse.
Most url methods provide four base authentication/header overloads (note that postDataAsObject only has the single (url, data) signature; base postform overloads accept an object or a JSON object string such as '{"name":"abc"}', not a=1&b=2, while the 4-arg cookies overload accepts object only):
| Form | Parameters | Description |
|---|---|---|
| No auth | url (and optional data) | URL only |
| Bearer | url, [data,] token | Automatically sets Authorization: Bearer |
| Basic | url, [data,] username, password | HTTP Basic authentication |
| Custom headers | url, [data,] headers | Header dictionary, such as { Authorization: "Bearer xxx" } |
When both headers and cookies (url, [data,] headers, cookies) are passed, get, post, put, delete, patch, and postform return a full CurlResponse object.
GET
Execute a synchronous HTTP GET request returning text, parsed objects, or raw bytes.
| Method | Parameters | Return | Description |
|---|---|---|---|
get(url, ...) | url: string and optional auth/headers/cookies | string or CurlResponse | Response body text (3-arg cookies overload returns CurlResponse) |
getAsObject(url, ...) | url: string and optional auth/headers | any | Parse the response into a JavaScript object |
getAsBinary(url, ...) | url: string and optional auth/headers | number[] | null | Response bytes (fetch images and other binary data) |
getJson(url, ...) | url: string and optional auth/headers | any | Parse the response as JSON |
Returns: string, any, number[] | null, or CurlResponse depending on the method.
k.api.get("fetchImage", () => {
const bytes = k.net.url.getAsBinary("https://www.kooboo.com/img/logo-white.png")
return { length: bytes ? bytes.length : 0 }
})
k.api.get("fetchJson", () => {
const data = k.net.url.getJson("https://httpbin.org/json")
return { hasSlides: data && !!data.slideshow }
})POST
Execute a synchronous HTTP POST request with a string body, JSON payload, or form parameters.
| Method | Parameters | Return | Description |
|---|---|---|---|
post(url, data, ...) | url: string, data: string and optional auth/headers/cookies | string or CurlResponse | POST string body (4-arg cookies overload returns CurlResponse) |
postAsObject(url, data, ...) | url: string, data: string and optional auth/headers | any | POST string body and parse response as object |
postAsBinary(url, data, ...) | url: string, data: string and optional auth/headers | number[] | null | POST string body and return response bytes |
postData(url, data, ...) | url: string, data: any and optional auth/headers | string | Serialize data as JSON (Content-Type: application/json) and POST |
postDataAsObject(url, data) | url: string, data: any | any | Single (url, data) overload; POSTs JSON and parses response as object |
postform(url, data, ...) | url: string, data: string | object and optional auth/headers/cookies (cookies overload accepts object only) | string or CurlResponse | application/x-www-form-urlencoded form POST (when passing a string, it must be a JSON object string such as '{"name":"abc"}', not a=1&b=2; 4-arg cookies overload returns CurlResponse) |
Returns: string, any, number[] | null, or CurlResponse depending on the method.
k.api.post("echo", () => {
const body = k.net.url.post("https://httpbin.org/post", "hello=world")
const jsonResp = k.net.url.postData("https://httpbin.org/post", { ok: true })
return {
containsHello: body && body.indexOf("hello") > -1,
sentJson: jsonResp && jsonResp.indexOf("true") > -1
}
})PUT / PATCH / DELETE
Execute synchronous HTTP PUT, PATCH, or DELETE requests.
| Verb | String response | Object response | CurlResponse overload (with cookies) |
|---|---|---|---|
| PUT | put(url, data, ...) | putAsObject(url, data, ...) | put(url, data, headers, cookies) |
| PATCH | patch(url, data, ...) | patchAsObject(url, data, ...) | patch(url, data, headers, cookies) |
| DELETE | delete(url, data, ...) | deleteAsObject(url, data, ...) | delete(url, data, headers, cookies) |
patchData(url, data, ...) behaves like postData, serializing data as JSON (Content-Type: application/json).
Content-Type and the headers dictionary
post, put, patch, and delete (including postData/patchData) automatically set Content-Type: application/json. The headers dictionary only accepts HTTP request headers—do not pass Content-Type or other content headers there, or HttpRequestMessage.Headers.Add will throw and return the exception message without sending the request.
Returns: string, any, or CurlResponse depending on the overload.
const updated = k.net.url.put("https://httpbin.org/put", JSON.stringify({ active: true }), {
"X-Request-Id": "req-1"
})Cookie Overloads & CurlResponse
Passing headers and cookies dictionaries returns CurlResponse so you can inspect status codes, headers, and raw/text bodies:
| Parameter | Type | Required | Description |
|---|---|---|---|
url | string | Yes | Target absolute URL |
data | string | any | Required for non-GET | Request payload (string for post/put/patch/delete; postform requires an object—passing a string double-serializes and throws) |
headers | Record<string, any> | Yes | Request header dictionary |
cookies | Record<string, any> | Yes | Cookie key-value dictionary |
Returns: CurlResponse.
const res = k.net.url.get(
"https://httpbin.org/cookies",
{ "X-Custom": "1" },
{ session: "abc" }
)
return {
status: res.httpStatusCode,
body: res.bodyString(),
headers: res.headers
}CurlResponse Properties
| Member | Type | Description |
|---|---|---|
data | number[] | Raw response bytes |
headers | Record<string, any> | Response headers map |
httpStatusCode | number | Numeric HTTP status code |
httpStatusCodeString | string | HTTP status description string |
size | number | Response size in bytes |
requestTime | number | Request duration |
errorMessage | string | Error message, if any |
bodyString() | () => string | Decode response bytes as a string |
Other Methods
| Method | Parameters | Return | Description |
|---|---|---|---|
sendMessage(requestMessage, data) | any, Record<string, any> | CurlResponse | Send a request described by data; pass k.request as requestMessage to forward form and files when using multipart/form-data |
downloadZip(url) | string | void | Fetch remote ZIP bytes and write directly to the current HTTP response (Content-Type: application/zip, forwarding Content-Disposition; throws if Content-Disposition is missing) |
sendMessage(requestMessage, data) parameters:
requestMessage: the currentk.requestobject; used only whendata.headers["Content-Type"]is"multipart/form-data"to forwardk.request.formfields andk.request.files(passk.requestornullfor non-multipart requests).data: request descriptor object{ url, method, params?, headers?, body?, multipartpartkey? }:urlandmethodare required;paramsmerges query parameters ontourl;headers["Content-Type"]only handles"application/json","text/plain", and"multipart/form-data";bodyprovides the request payload (object or plain text);multipartpartkeyspecifies a form field key to omit when forwardingmultipart/form-data.
Example: Proxy vs. Binary Helpers
// Recommended: proxy a third-party API using an allowlisted upstream URL
const allowedTargets = {
items: "https://api.example.com/v1/items",
orders: "https://api.example.com/v1/orders"
}
k.api.post("proxyApi", () => {
const targetUrl = allowedTargets[k.request.queryString.endpoint]
if (!targetUrl) {
k.response.statusCode(400)
return { error: "Invalid upstream endpoint" }
}
const payload = k.request.body ? JSON.parse(k.request.body) : {}
const content = k.net.httpClient.createJsonContent(payload)
const headers = {}
if (k.request.headers.authorization) {
headers.Authorization = k.request.headers.authorization
}
const res = k.net.httpClient.send(targetUrl, "post", content, headers)
return { status: res.statusCode, body: res.bodyString() }
})
// Quick path: fetch remote image bytes and write them to the media library
k.api.post("importLogo", () => {
const bytes = k.net.url.getAsBinary("https://www.kooboo.com/img/logo-white.png")
k.media.writeBinary("/import/logo-white.png", bytes, true)
return { imported: k.media.exists("/import/logo-white.png") }
})TypeScript Definitions
Show full TypeScript definitions
interface Curl {
get(url: string, headers: Record<string, any>, cookies: Record<string, any>): CurlResponse;
post(url: string, data: string, headers: Record<string, any>, cookies: Record<string, any>): CurlResponse;
put(url: string, data: string, headers: Record<string, any>, cookies: Record<string, any>): CurlResponse;
delete(url: string, data: string, headers: Record<string, any>, cookies: Record<string, any>): CurlResponse;
patch(url: string, data: string, headers: Record<string, any>, cookies: Record<string, any>): CurlResponse;
postform(url: string, data: any, headers: Record<string, any>, cookies: Record<string, any>): CurlResponse;
get(url: string): string;
get(url: string, token: string): string;
get(url: string, username: string, password: string): string;
get(url: string, headers: Record<string, any>): string;
getAsObject(url: string): any;
getAsObject(url: string, token: string): any;
getAsObject(url: string, username: string, password: string): any;
getAsObject(url: string, headers: Record<string, any>): any;
getAsBinary(url: string): number[];
getAsBinary(url: string, token: string): number[];
getAsBinary(url: string, username: string, password: string): number[];
getAsBinary(url: string, headers: Record<string, any>): number[];
getJson(url: string): any;
getJson(url: string, token: string): any;
getJson(url: string, username: string, password: string): any;
getJson(url: string, headers: Record<string, any>): any;
post(url: string, data: string): string;
post(url: string, data: string, token: string): string;
post(url: string, data: string, userName: string, password: string): string;
post(url: string, data: string, headers: Record<string, any>): string;
postAsObject(url: string, data: string): any;
postAsObject(url: string, data: string, token: string): any;
postAsObject(url: string, data: string, userName: string, password: string): any;
postAsObject(url: string, data: string, headers: Record<string, any>): any;
postAsBinary(url: string, data: string): any;
postAsBinary(url: string, data: string, token: string): any;
postAsBinary(url: string, data: string, userName: string, password: string): any;
postAsBinary(url: string, data: string, headers: Record<string, any>): any;
postData(url: string, data: any): string;
postData(url: string, data: any, token: string): string;
postData(url: string, data: any, userName: string, password: string): string;
postData(url: string, data: any, headers: Record<string, any>): string;
postDataAsObject(url: string, data: any): any;
put(url: string, data: string): string;
put(url: string, data: string, token: string): string;
put(url: string, data: string, userName: string, password: string): string;
put(url: string, data: string, headers: Record<string, any>): string;
putAsObject(url: string, data: string): any;
putAsObject(url: string, data: string, token: string): any;
putAsObject(url: string, data: string, userName: string, password: string): any;
putAsObject(url: string, data: string, headers: Record<string, any>): any;
delete(url: string, data: string): string;
delete(url: string, data: string, token: string): string;
delete(url: string, data: string, userName: string, password: string): string;
delete(url: string, data: string, headers: Record<string, any>): string;
deleteAsObject(url: string, data: string): any;
deleteAsObject(url: string, data: string, token: string): any;
deleteAsObject(url: string, data: string, userName: string, password: string): any;
deleteAsObject(url: string, data: string, headers: Record<string, any>): any;
patch(url: string, data: string): string;
patch(url: string, data: string, token: string): string;
patch(url: string, data: string, userName: string, password: string): string;
patch(url: string, data: string, headers: Record<string, any>): string;
patchAsObject(url: string, data: string): any;
patchAsObject(url: string, data: string, token: string): any;
patchAsObject(url: string, data: string, userName: string, password: string): any;
patchAsObject(url: string, data: string, headers: Record<string, any>): any;
patchData(url: string, data: any): string;
patchData(url: string, data: any, token: string): string;
patchData(url: string, data: any, userName: string, password: string): string;
patchData(url: string, data: any, headers: Record<string, any>): string;
postform(url: string, data: string): string;
postform(url: string, data: any): string;
postform(url: string, data: string, token: string): string;
postform(url: string, data: any, token: string): string;
postform(url: string, data: string, userName: string, password: string): string;
postform(url: string, data: any, userName: string, password: string): string;
postform(url: string, data: string, headers: Record<string, any>): string;
postform(url: string, data: any, headers: Record<string, any>): string;
downloadZip(url: string): void;
sendMessage(requestMessage: any, data: Record<string, any>): CurlResponse;
}
interface CurlResponse {
data: number[];
headers: Record<string, any>;
httpStatusCode: number;
httpStatusCodeString: string;
size: number;
requestTime: number;
errorMessage: string;
bodyString(): string;
}