httpclient
nimony/lib/std/httpclient.nim
HTTP/1.1 requests as streamio constructors (nim-lang/nimony#2635).
The constructor does the talking — request out, status and headers in — and hands back the response BODY as a Reader:
.. code-block:: nim var page = httpGet("https://example.com/") echo page.readAll() var r = httpPost("https://api.example.com/upload", openReader(path"big.bin"))
httpsin the URL means TLS (std/tls'snewTlsEngine()unless an
engine is passed). An engine on a plain http URL raises.
- A status outside 2xx RAISES the
ErrorCodehttpToErrorCodemaps it to
(404 is NameNotFound, 401/403 PermissionDenied, ...), so a Reader returned from here always carries a successful body.
- The body is de-framed (
Content-Length, chunked, or until the server
closes) and its content coding undone (gzip, deflate) by an adapter in the reader's adapter slot. Trailers are read and dropped.
- Request headers are an
HttpHeaders, the wire format itself. The library
writes Host, Content-Length/Transfer-Encoding and Connection itself; a user header with one of those names raises BadOperation (conflicting framing headers are how request smuggling works). User-Agent and Accept-Encoding have defaults a user header replaces.
One request per connection (Connection: close); redirects are raised (RedirectError), not followed.
func dollar`.Framing(e: Framing): stringfunc dollar`.ChunkState(e: ChunkState): stringproc httpGet(url: string; headers: HttpHeaders; response: out HttpHeaders; tls: ref TlsEngine.Obj): ReaderThe body of
url; the response headers inresponse.proc httpGet(url: string; headers: HttpHeaders; tls: ref TlsEngine.Obj): ReaderThe body of
url.proc httpPost(url: string; body: sink Reader; contentType: string; headers: HttpHeaders; tls: ref TlsEngine.Obj): ReaderSends
body(streamed, chunked) and returns the response body.proc httpPost(url: string; body: sink Reader; contentType: string; headers: HttpHeaders; response: out HttpHeaders; tls: ref TlsEngine.Obj): Readerproc httpPut(url: string; body: sink Reader; contentType: string; headers: HttpHeaders; tls: ref TlsEngine.Obj): Reader