diff options
author | Dominik Picheta <dominikpicheta@gmail.com> | 2016-09-24 17:50:58 +0200 |
---|---|---|
committer | Dominik Picheta <dominikpicheta@gmail.com> | 2016-09-24 17:50:58 +0200 |
commit | fa9ec7a6b54ae1fc59674f530a0ee4a7532ca5e7 (patch) | |
tree | a0db9e1f17f40166eee0816299e877ce93a8479f /lib | |
parent | 547fb7f1e4a37ee759171834ad651d68561a7c5d (diff) | |
download | Nim-fa9ec7a6b54ae1fc59674f530a0ee4a7532ca5e7.tar.gz |
Handle redirects in HttpClient's post procs & post test.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/httpclient.nim | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim index 27b3b46be..5eeb54f32 100644 --- a/lib/pure/httpclient.nim +++ b/lib/pure/httpclient.nim @@ -964,6 +964,8 @@ proc get*(client: HttpClient | AsyncHttpClient, ## This procedure will follow redirects up to a maximum number of redirects ## specified in ``client.maxRedirects``. result = await client.request(url, HttpGET) + + # Handle redirects. var lastURL = url for i in 1..client.maxRedirects: if result.status.redirection(): @@ -990,3 +992,11 @@ proc post*(client: HttpClient | AsyncHttpClient, url: string, body = "", client.headers["Content-Length"] = $len(xb) result = await client.request(url, HttpPOST, xb) + # Handle redirects. + var lastURL = url + for i in 1..client.maxRedirects: + if result.status.redirection(): + let redirectTo = getNewLocation(lastURL, result.headers) + var meth = if result.status != "307": HttpGet else: HttpPost + result = await client.request(redirectTo, meth, xb) + lastURL = redirectTo |