Example: cmd: --gc:orc --experimental:strictFuncs -d:ssl -d:nimStressOrc --import:std/httpcore
import src/harpoon import std/asyncdispatch # Async works. Can work with Threads, Tasks, Macros, etc. import std/json # GET and POST from JSON to JSON directly. from std/uri import parseUri # To use Uri. block: doAssert get(parseUri"http://httpbin.org/get").code == Http200 doAssert getContent(parseUri"http://httpbin.org/get").len > 0 block: doAssert post(parseUri"http://httpbin.org/post", "data here").code == Http200 doAssert postContent(parseUri"http://httpbin.org/post", "data here").len > 0 block: doAssert delete(parseUri"http://httpbin.org/delete").code == Http200 doAssert deleteContent(parseUri"http://httpbin.org/delete").len > 0 block: doAssert put(parseUri"http://httpbin.org/put", "data here").code == Http200 doAssert putContent(parseUri"http://httpbin.org/put", "data here").len > 0 block: doAssert patch(parseUri"http://httpbin.org/patch", "data here").code == Http200 doAssert patchContent(parseUri"http://httpbin.org/patch", "data here").len > 0 block: let jsonData: JsonNode = %*{"key": "value", "other": 42} # GET and POST from JSON to JSON doAssert getJson(parseUri"http://httpbin.org/get") is JsonNode doAssert postJson(parseUri"http://httpbin.org/post", jsonData) is JsonNode block: downloadFile parseUri"http://httpbin.org/image/png", "temp.png" # Download 1 or multiple files downloadFile [(url: parseUri"http://httpbin.org/image/png", path: "temp.png"), (url: parseUri"http://httpbin.org/image/jpg", path: "temp.jpg")] doAssert newDefaultHeaders(body = "data here", proxyUser = "root", proxyPassword = "password") is array[5, (string, string)] block: proc example() {.async, used.} = doAssert getContent("http://httpbin.org/get") is Future[string] doAssert deleteContent("http://httpbin.org/delete") is Future[string] doAssert putContent("http://httpbin.org/put", "data here") is Future[string] doAssert postContent("http://httpbin.org/post", "data here") is Future[string] doAssert patchContent("http://httpbin.org/patch", "data here") is Future[string] doAssert downloadFile("http://httpbin.org/image/png", "temp.png") is Future[void] waitFor example()
Procs
proc fetch(socket: Socket; url: Uri; metod: HttpMethod; headers: openArray[(string, string)]; body = ""; timeout = -1; proxyUrl = ""; port = 80.Port; portSsl = 442.Port; parseHeader = true; parseStatus = true; parseBody = true; ignoreErrors = false; bodyOnly: static[bool] = false): auto {. ...raises: [IOError, OSError, TimeoutError, SslError, ValueError].}
proc fetch(socket: AsyncSocket; url: string; metod: HttpMethod; headers: seq[(string, string)]; body = ""; timeout = -1; proxyUrl = ""; port = 80.Port; portSsl = 442.Port; parseHeader = true; parseStatus = true; parseBody = true; ignoreErrors = false): Future[tuple[url: Uri, metod: HttpMethod, isIpv6: bool, headers: seq[(string, string)], code: HttpCode, body: string]] {. ...raises: [IOError, OSError, SslError, ValueError, Exception], tags: [TimeEffect, RootEffect, ReadDirEffect, ReadEnvEffect].}
proc get(url: Uri): auto {....raises: [OSError, IOError, OSError, TimeoutError, SslError, ValueError, Exception, LibraryError], tags: [RootEffect, ReadDirEffect, ReadEnvEffect, ReadIOEffect, WriteIOEffect, TimeEffect].}
proc getContent(url: Uri): string {....raises: [OSError, IOError, OSError, TimeoutError, SslError, ValueError, Exception, LibraryError], tags: [ RootEffect, ReadDirEffect, ReadEnvEffect, ReadIOEffect, WriteIOEffect, TimeEffect].}
proc post(url: Uri; body: string): auto {....raises: [OSError, IOError, OSError, TimeoutError, SslError, ValueError, Exception, LibraryError], tags: [ RootEffect, ReadDirEffect, ReadEnvEffect, ReadIOEffect, WriteIOEffect, TimeEffect].}
proc postContent(url: Uri; body: string): string {....raises: [OSError, IOError, OSError, TimeoutError, SslError, ValueError, Exception, LibraryError], tags: [ RootEffect, ReadDirEffect, ReadEnvEffect, ReadIOEffect, WriteIOEffect, TimeEffect].}
proc put(url: Uri; body: string): auto {....raises: [OSError, IOError, OSError, TimeoutError, SslError, ValueError, Exception, LibraryError], tags: [ RootEffect, ReadDirEffect, ReadEnvEffect, ReadIOEffect, WriteIOEffect, TimeEffect].}
proc putContent(url: Uri; body: string): string {....raises: [OSError, IOError, OSError, TimeoutError, SslError, ValueError, Exception, LibraryError], tags: [ RootEffect, ReadDirEffect, ReadEnvEffect, ReadIOEffect, WriteIOEffect, TimeEffect].}
proc patch(url: Uri; body: string): auto {....raises: [OSError, IOError, OSError, TimeoutError, SslError, ValueError, Exception, LibraryError], tags: [ RootEffect, ReadDirEffect, ReadEnvEffect, ReadIOEffect, WriteIOEffect, TimeEffect].}
proc patchContent(url: Uri; body: string): string {....raises: [OSError, IOError, OSError, TimeoutError, SslError, ValueError, Exception, LibraryError], tags: [ RootEffect, ReadDirEffect, ReadEnvEffect, ReadIOEffect, WriteIOEffect, TimeEffect].}
proc delete(url: Uri): auto {....raises: [OSError, IOError, OSError, TimeoutError, SslError, ValueError, Exception, LibraryError], tags: [RootEffect, ReadDirEffect, ReadEnvEffect, ReadIOEffect, WriteIOEffect, TimeEffect].}
proc deleteContent(url: Uri): string {....raises: [OSError, IOError, OSError, TimeoutError, SslError, ValueError, Exception, LibraryError], tags: [ RootEffect, ReadDirEffect, ReadEnvEffect, ReadIOEffect, WriteIOEffect, TimeEffect].}
proc downloadFile(url: Uri; filename: string) {....raises: [OSError, IOError, OSError, TimeoutError, SslError, ValueError, Exception, LibraryError], tags: [ WriteIOEffect, RootEffect, ReadDirEffect, ReadEnvEffect, ReadIOEffect, TimeEffect].}
proc getContent(url: string): Future[string] {....raises: [Exception, ValueError], tags: [RootEffect, TimeEffect, ReadDirEffect, ReadEnvEffect].}
proc postContent(url: string; body: string): Future[string] {. ...raises: [Exception, ValueError], tags: [RootEffect, TimeEffect, ReadDirEffect, ReadEnvEffect].}
proc putContent(url: string; body: string): Future[string] {. ...raises: [Exception, ValueError], tags: [RootEffect, TimeEffect, ReadDirEffect, ReadEnvEffect].}
proc patchContent(url: string; body: string): Future[string] {. ...raises: [Exception, ValueError], tags: [RootEffect, TimeEffect, ReadDirEffect, ReadEnvEffect].}
proc deleteContent(url: string): Future[string] {. ...raises: [Exception, ValueError], tags: [RootEffect, TimeEffect, ReadDirEffect, ReadEnvEffect].}
proc downloadFile(url: string; filename: string): Future[void] {. ...raises: [Exception], tags: [RootEffect, WriteIOEffect, TimeEffect, ReadDirEffect, ReadEnvEffect].}
proc getJson(url: Uri): JsonNode {....raises: [OSError, IOError, OSError, JsonParsingError, ValueError, Exception, TimeoutError, SslError, LibraryError], tags: [ReadIOEffect, WriteIOEffect, RootEffect, ReadDirEffect, ReadEnvEffect, TimeEffect].}
proc postJson(url: Uri; body: JsonNode): JsonNode {....raises: [OSError, IOError, OSError, JsonParsingError, ValueError, Exception, TimeoutError, SslError, LibraryError], tags: [ReadIOEffect, WriteIOEffect, RootEffect, ReadDirEffect, ReadEnvEffect, TimeEffect].}
proc downloadFile(files: openArray[tuple[url: Uri, path: string]]) {....raises: [ OSError, IOError, OSError, TimeoutError, SslError, ValueError, Exception, LibraryError], tags: [WriteIOEffect, RootEffect, ReadDirEffect, ReadEnvEffect, ReadIOEffect, TimeEffect].}
Templates
template newDefaultHeaders(body: string; contentType = "text/plain"; accept = "*/*"; userAgent = "x"): array[4, (string, string)]
template newDefaultHeaders(body: string; doNotTrack: bool; contentType = "application/json"; accept = "application/json"; userAgent = "x"): array[ 5, (string, string)]
template newDefaultHeaders(body: string; proxyUser: string; proxyPassword: string; contentType = "text/plain"; accept = "*/*"; userAgent = "x"): array[5, (string, string)]