Skip to content

k.net.url ​

Easy synchronous HTTP requests (string/object/binary)

Overview ​

k.net.url initiates a synchronous HTTP call to the remote URL and directly returns a string, object or byte[] according to the method name. The writing method is short and suitable for quickly grabbing content within the script.

When to use httpClient and when to use url

  • Proxy forwarding, complex header, multipart, require KHttpResponse disk placement or status code judgment → use k.net.httpClient (more complete, configurable timeout and batch tasks).
  • Quickly pull the binary of a remote image or file → k.net.url.getAsBinary(url) is often the most straightforward.
  • One line of GET text or JSON → k.net.url.get / k.net.url.getJson.

The same method usually provides four authentication/header forms (besides just URL):

formParameterDescription
No authenticationurlURL only
Bearerurl, tokenAutomatically add Bearer
Basicurl, username, passwordHTTP Basic
Custom headerurl, headersDictionary, such as { Authentication: "Bearer xxx" }

The method names in the following sections all support the four overloads in the above table (the document does not list them repeatedly).

GET ​

MethodReturnDescription
getstringResponse body text
getAsObjectobjectParse the response into a JavaScript object
getAsBinarynumber[]Response bytes (Pull images and other binary)
getJsonobjectParse the response as JSON
ts
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 ​

MethodReturnDescription
poststringPOST string body
postAsObjectobjectResponse parsed into object
postAsBinaryobjectReturn in object form after POST (see runtime behavior)
postDatastringPOST serialized object (form/JSON is implementation determined)
postDataAsObjectobjectpostData and the response is an object
postformstringapplication/x-www-form-urlencoded Form POST
ts
k.api.post("echo", () => {
    const body = k.net.url.post("https://httpbin.org/post", "hello=world")
    return { containsHello: body && body.indexOf("hello") > -1 }
})

PUT/PATCH/DELETE ​

verbstring responseobject response
PUTputputAsObject
PATCHpatchpatchAsObject
DELETEdeletedeleteAsObject

patchData is similar to postData and is used to submit the object body.

other ​

MethodDescription
downloadZip(url)Download ZIP (specific behavior is subject to console/site configuration, please verify in the target environment before use)

Example: Agent vs. Binary Division of Labor ​

ts
// Recommended: proxy a third-party API
k.api.post("proxyApi", () => {
    const content = k.net.httpClient.createJsonContent(k.request.body)
    const res = k.net.httpClient.send(k.request.queryString.target, "post", content, {
        Authorization: k.request.headers.authorization
    })
    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") }
})