Skip to content

k.net.url ​

Easy synchronous HTTP requests (string / object / binary / full response)

Execution ContextPrerequisitesTypical Use
KScript API, Page / View server scriptsOutbound HTTP access; use httpClient for complex proxying or batch tasksOne-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 ​

js
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 / PropertyDescriptionReturns
get / getAsObject / getJson / getAsBinarySynchronous GET as string, object, JSON, or bytesstring / any / number[] / CurlResponse
post / postAsObject / postAsBinary / postData / postDataAsObjectSynchronous POST with string or JSON bodystring / any / number[] / CurlResponse
postform(url, data, ...)Submit application/x-www-form-urlencoded form datastring / CurlResponse
put / putAsObject / patch / patchAsObject / patchData / delete / deleteAsObjectSynchronous PUT, PATCH, and DELETE requestsstring / 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 responsevoid

When to use httpClient vs url

  • Proxy forwarding, complex headers, multipart uploads, custom timeouts, KHttpResponse file 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 cookies overloads on get / post / put / delete / patch / postform, or sendMessage(), which all return CurlResponse.

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):

FormParametersDescription
No authurl (and optional data)URL only
Bearerurl, [data,] tokenAutomatically sets Authorization: Bearer
Basicurl, [data,] username, passwordHTTP Basic authentication
Custom headersurl, [data,] headersHeader 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.

MethodParametersReturnDescription
get(url, ...)url: string and optional auth/headers/cookiesstring or CurlResponseResponse body text (3-arg cookies overload returns CurlResponse)
getAsObject(url, ...)url: string and optional auth/headersanyParse the response into a JavaScript object
getAsBinary(url, ...)url: string and optional auth/headersnumber[] | nullResponse bytes (fetch images and other binary data)
getJson(url, ...)url: string and optional auth/headersanyParse the response as JSON

Returns: string, any, number[] | null, or CurlResponse depending on the method.

js
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.

MethodParametersReturnDescription
post(url, data, ...)url: string, data: string and optional auth/headers/cookiesstring or CurlResponsePOST string body (4-arg cookies overload returns CurlResponse)
postAsObject(url, data, ...)url: string, data: string and optional auth/headersanyPOST string body and parse response as object
postAsBinary(url, data, ...)url: string, data: string and optional auth/headersnumber[] | nullPOST string body and return response bytes
postData(url, data, ...)url: string, data: any and optional auth/headersstringSerialize data as JSON (Content-Type: application/json) and POST
postDataAsObject(url, data)url: string, data: anyanySingle (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 CurlResponseapplication/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.

js
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.

VerbString responseObject responseCurlResponse overload (with cookies)
PUTput(url, data, ...)putAsObject(url, data, ...)put(url, data, headers, cookies)
PATCHpatch(url, data, ...)patchAsObject(url, data, ...)patch(url, data, headers, cookies)
DELETEdelete(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.

js
const updated = k.net.url.put("https://httpbin.org/put", JSON.stringify({ active: true }), {
  "X-Request-Id": "req-1"
})

Passing headers and cookies dictionaries returns CurlResponse so you can inspect status codes, headers, and raw/text bodies:

ParameterTypeRequiredDescription
urlstringYesTarget absolute URL
datastring | anyRequired for non-GETRequest payload (string for post/put/patch/delete; postform requires an object—passing a string double-serializes and throws)
headersRecord<string, any>YesRequest header dictionary
cookiesRecord<string, any>YesCookie key-value dictionary

Returns: CurlResponse.

js
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 ​

MemberTypeDescription
datanumber[]Raw response bytes
headersRecord<string, any>Response headers map
httpStatusCodenumberNumeric HTTP status code
httpStatusCodeStringstringHTTP status description string
sizenumberResponse size in bytes
requestTimenumberRequest duration
errorMessagestringError message, if any
bodyString()() => stringDecode response bytes as a string

Other Methods ​

MethodParametersReturnDescription
sendMessage(requestMessage, data)any, Record<string, any>CurlResponseSend a request described by data; pass k.request as requestMessage to forward form and files when using multipart/form-data
downloadZip(url)stringvoidFetch 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 current k.request object; used only when data.headers["Content-Type"] is "multipart/form-data" to forward k.request.form fields and k.request.files (pass k.request or null for non-multipart requests).
  • data: request descriptor object { url, method, params?, headers?, body?, multipartpartkey? }: url and method are required; params merges query parameters onto url; headers["Content-Type"] only handles "application/json", "text/plain", and "multipart/form-data"; body provides the request payload (object or plain text); multipartpartkey specifies a form field key to omit when forwarding multipart/form-data.

Example: Proxy vs. Binary Helpers ​

js
// 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
ts
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;
}