diff options
author | Dominik Picheta <dominikpicheta@googlemail.com> | 2015-07-01 21:11:14 +0100 |
---|---|---|
committer | Dominik Picheta <dominikpicheta@googlemail.com> | 2015-07-01 21:11:14 +0100 |
commit | cb34e3721e014facda8dd8b874c5606b8e6b19cb (patch) | |
tree | e385bbc9932d45247bd8c8ce4ee80f07c47941c3 | |
parent | cb5d090cdb0318542804df7f122475b7de5410a6 (diff) | |
parent | 02402d3ffd1027d7f4de40a6cbb80cf1fba3e1c7 (diff) | |
download | Nim-cb34e3721e014facda8dd8b874c5606b8e6b19cb.tar.gz |
Merge pull request #3037 from rgv151/patch-3
Implement async `post` request
-rw-r--r-- | lib/pure/httpclient.nim | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim index 6a2913713..2ca2098b3 100644 --- a/lib/pure/httpclient.nim +++ b/lib/pure/httpclient.nim @@ -821,6 +821,24 @@ proc get*(client: AsyncHttpClient, url: string): Future[Response] {.async.} = result = await client.request(redirectTo, httpGET) lastUrl = redirectTo +proc post*(client: AsyncHttpClient, url: string, body = "", multipart: MultipartData = nil): Future[Response] {.async.} = + ## Connects to the hostname specified by the URL and performs a POST request. + ## + ## This procedure will follow redirects up to a maximum number of redirects + ## specified in ``newAsyncHttpClient``. + let (mpHeader, mpBody) = format(multipart) + + template withNewLine(x): expr = + if x.len > 0 and not x.endsWith("\c\L"): + x & "\c\L" + else: + x + var xb = mpBody.withNewLine() & body + client.headers["Content-Type"] = mpHeader.split(": ")[1] + client.headers["Content-Length"] = $len(xb) + + result = await client.request(url, httpPOST, xb) + when not defined(testing) and isMainModule: when true: # Async |