diff options
Diffstat (limited to 'lib/pure/httpclient.nim')
-rw-r--r-- | lib/pure/httpclient.nim | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim index 4404a9426..c56d13b57 100644 --- a/lib/pure/httpclient.nim +++ b/lib/pure/httpclient.nim @@ -18,14 +18,14 @@ ## ## .. code-block:: Nim ## var client = newHttpClient() -## echo(getContent("http://google.com")) +## echo client.getContent("http://google.com") ## ## The same action can also be performed asynchronously, simply use the ## ``AsyncHttpClient``: ## ## .. code-block:: Nim ## var client = newAsyncHttpClient() -## echo(await getContent("http://google.com")) +## echo await client.getContent("http://google.com") ## ## The functionality implemented by ``HttpClient`` and ``AsyncHttpClient`` ## is the same, so you can use whichever one suits you best in the examples @@ -50,6 +50,20 @@ ## ## echo client.postContent("http://validator.w3.org/check", multipart=data) ## +## You can also make post requests with custom headers. +## This example sets ``Content-Type`` to ``application/json`` +## and uses a json object for the body +## +## .. code-block:: Nim +## import httpclient, json +## +## let client = newHttpClient() +## client.headers = newHttpHeaders({ "Content-Type": "application/json" }) +## let body = %*{ +## "data": "some text" +## } +## echo client.request("http://some.api", httpMethod = HttpPost, body = $body) +## ## Progress reporting ## ================== ## @@ -432,7 +446,7 @@ proc request*(url: string, httpMethod: string, extraHeaders = "", # get the socket ready. If we are connecting through a proxy to SSL, - # send the appropiate CONNECT header. If not, simply connect to the proper + # send the appropriate CONNECT header. If not, simply connect to the proper # host (which may still be the proxy, for normal HTTP) if proxy != nil and hostUrl.scheme == "https": when defined(ssl): @@ -736,7 +750,7 @@ proc newHttpClient*(userAgent = defUserAgent, ## ``proxy`` specifies an HTTP proxy to use for this HTTP client's ## connections. ## - ## ``timeout`` specifies the number of miliseconds to allow before a + ## ``timeout`` specifies the number of milliseconds to allow before a ## ``TimeoutError`` is raised. new result result.headers = newHttpHeaders() @@ -944,8 +958,10 @@ proc parseResponse(client: HttpClient | AsyncHttpClient, proc newConnection(client: HttpClient | AsyncHttpClient, url: Uri) {.multisync.} = if client.currentURL.hostname != url.hostname or - client.currentURL.scheme != url.scheme: - if client.connected: client.close() + client.currentURL.scheme != url.scheme or + client.currentURL.port != url.port: + if client.connected: + client.close() when client is HttpClient: client.socket = newSocket() |