src/nodejs/jshttpclient

    Dark Mode
Search:
Group by:

Warning: Experimental JavaScript API.
  • An HttpClient for JavaScript targets implemented as sugar on top of jsxmlhttprequest and std/jsfetch.

Example: cmd: -r:off

import src/nodejs/jshttpclient
import std/uri

let client: JsHttpClient = newJsHttpClient()
const data: cstring = """{"key": "value"}"""

block:
  let
    url: Uri = parseUri("https://google.com")
    content: cstring = client.getContent(url)

block:
  let
    url: Uri = parseUri("https://httpbin.org/delete")
    content: cstring = client.deleteContent(url)

block:
  let
    url: Uri = parseUri("https://httpbin.org/post")
    content: cstring = client.postContent(url, data)

block:
  let
    url: Uri = parseUri("https://httpbin.org/put")
    content: cstring = client.putContent(url, data)

block:
  let
    url: Uri = parseUri("https://httpbin.org/patch")
    content: cstring = client.patchContent(url, data)

Example: cmd: -d:nimExperimentalJsfetch -d:nimExperimentalAsyncjsThen -r:off

import src/nodejs/jshttpclient
import std/[asyncjs, uri]

proc example(): Future[void] {.async.} =
  let client: JsAsyncHttpClient = newJsAsyncHttpClient()
  const data = """{"key": "value"}"""

  block:
    let
      url: Uri = parseUri("http://nim-lang.org")
      content: cstring = await client.getContent(url)
      response: JsResponse = await client.get(url)

  block:
    let
      url: Uri = parseUri("http://httpbin.org/delete")
      content: cstring = await client.deleteContent(url)
      response: JsResponse = await client.delete(url)

  block:
    let
      url: Uri = parseUri("http://httpbin.org/post")
      content: cstring = await client.postContent(url, data)
      response: JsResponse = await client.post(url, data)

  block:
    let
      url: Uri = parseUri("http://httpbin.org/put")
      content: cstring = await client.putContent(url, data)
      response: JsResponse = await client.put(url, data)

  block:
    let
      url: Uri = parseUri("http://httpbin.org/patch")
      content: cstring = await client.patchContent(url, data)
      response: JsResponse = await client.patch(url, data)

discard example()

Types

JsAsyncHttpClient = ref object of JsRoot
  headers*: Headers
JsHttpClient = ref object of JsRoot
  http*: XMLHttpRequest
  headers*: Headers
JsRequest = ref object of JsRoot
  `method`*: HttpMethod
  url*, body*, integrity*, referrer*: cstring
  referrerPolicy*: FetchReferrerPolicies
  mode*: FetchModes
  credentials*: FetchCredentials
  cache*: FetchCaches
  redirect*: FetchRedirects
  keepAlive*: bool
JsResponse = ref object of JsRoot
  status*: HttpCode
  statusText*, url*, responseText*: cstring
  headers*: Headers

Procs

proc delete(client: JsHttpClient | JsAsyncHttpClient; url: Uri | string): Future[
    JsResponse] {.codegenDecl: "async function $2($3)".}
proc delete(client: JsHttpClient; url: Uri | string): JsResponse
proc deleteContent(client: JsHttpClient | JsAsyncHttpClient; url: Uri | string): Future[
    cstring] {.codegenDecl: "async function $2($3)".}
proc deleteContent(client: JsHttpClient; url: Uri | string): cstring
proc get(client: JsHttpClient | JsAsyncHttpClient; url: Uri | string): Future[
    JsResponse] {.codegenDecl: "async function $2($3)".}
proc get(client: JsHttpClient; url: Uri | string): JsResponse
proc getContent(client: JsHttpClient | JsAsyncHttpClient; url: Uri | string): Future[
    cstring] {.codegenDecl: "async function $2($3)".}
proc getContent(client: JsHttpClient; url: Uri | string): cstring
proc head(client: JsHttpClient | JsAsyncHttpClient; url: Uri | string): Future[
    JsResponse] {.codegenDecl: "async function $2($3)".}
proc head(client: JsHttpClient; url: Uri | string): JsResponse
func newJsAsyncHttpClient(headers: Headers = newHeaders()): JsAsyncHttpClient {.
    ...raises: [], tags: [].}
func newJsHttpClient(headers: Headers = newHeaders()): JsHttpClient {.
    ...raises: [], tags: [].}
proc newJsRequest(url: cstring; method: HttpMethod;
                  body, integrity, referrer: cstring = "";
                  referrerPolicy: FetchReferrerPolicies = frpOrigin;
                  mode: FetchModes = fmCors;
                  credentials: FetchCredentials = fcOmit;
                  cache: FetchCaches = fchDefault;
                  redirect: FetchRedirects = frFollow; keepAlive: bool = false): JsRequest {.
    ...raises: [], tags: [].}
proc patch(client: JsHttpClient | JsAsyncHttpClient; url: Uri | string;
           body: cstring = ""): Future[JsResponse] {.
    codegenDecl: "async function $2($3)".}
proc patch(client: JsHttpClient; url: Uri | string; body: cstring = ""): JsResponse
proc patchContent(client: JsHttpClient | JsAsyncHttpClient; url: Uri | string;
                  body: cstring = ""): Future[cstring] {.
    codegenDecl: "async function $2($3)".}
proc patchContent(client: JsHttpClient; url: Uri | string; body: cstring = ""): cstring
proc post(client: JsHttpClient | JsAsyncHttpClient; url: Uri | string;
          body: cstring = ""): Future[JsResponse] {.
    codegenDecl: "async function $2($3)".}
proc post(client: JsHttpClient; url: Uri | string; body: cstring = ""): JsResponse
proc postContent(client: JsHttpClient | JsAsyncHttpClient; url: Uri | string;
                 body: cstring = ""): Future[cstring] {.
    codegenDecl: "async function $2($3)".}
proc postContent(client: JsHttpClient; url: Uri | string; body: cstring = ""): cstring
proc put(client: JsHttpClient | JsAsyncHttpClient; url: Uri | string;
         body: cstring = ""): Future[JsResponse] {.
    codegenDecl: "async function $2($3)".}
proc put(client: JsHttpClient; url: Uri | string; body: cstring = ""): JsResponse
proc putContent(client: JsHttpClient | JsAsyncHttpClient; url: Uri | string;
                body: cstring = ""): Future[cstring] {.
    codegenDecl: "async function $2($3)".}
proc putContent(client: JsHttpClient; url: Uri | string; body: cstring = ""): cstring
proc request(client: JsAsyncHttpClient; request: JsRequest): Future[JsResponse] {.
    codegenDecl: "async function $2($3)", ...raises: [], tags: [].}
proc request(client: JsHttpClient; request: JsRequest): JsResponse {....raises: [],
    tags: [].}
Request proc for sync XMLHttpRequest client