diff options
-rw-r--r-- | lib/pure/httpclient.nim | 10 | ||||
-rw-r--r-- | tests/stdlib/thttpclient.nim | 35 |
2 files changed, 27 insertions, 18 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 diff --git a/tests/stdlib/thttpclient.nim b/tests/stdlib/thttpclient.nim index 9412a5afa..29590d9cf 100644 --- a/tests/stdlib/thttpclient.nim +++ b/tests/stdlib/thttpclient.nim @@ -20,6 +20,15 @@ proc asyncTest() {.async.} = resp = await client.request("https://google.com/") doAssert(resp.code.is2xx or resp.code.is3xx) + + # Multipart test. + var data = newMultipartData() + data["output"] = "soap12" + data["uploaded_file"] = ("test.html", "text/html", + "<html><head></head><body><p>test</p></body></html>") + resp = await client.post("http://validator.w3.org/check", multipart=data) + doAssert(resp.code.is2xx) + client.close() # Proxy test @@ -41,6 +50,14 @@ proc syncTest() = resp = client.request("https://google.com/") doAssert(resp.code.is2xx or resp.code.is3xx) + # Multipart test. + var data = newMultipartData() + data["output"] = "soap12" + data["uploaded_file"] = ("test.html", "text/html", + "<html><head></head><body><p>test</p></body></html>") + resp = client.post("http://validator.w3.org/check", multipart=data) + doAssert(resp.code.is2xx) + client.close() # Timeout test. @@ -56,21 +73,3 @@ proc syncTest() = syncTest() waitFor(asyncTest()) - -#[ - - else: - #downloadFile("http://force7.de/nim/index.html", "nimindex.html") - #downloadFile("http://www.httpwatch.com/", "ChunkTest.html") - #downloadFile("http://validator.w3.org/check?uri=http%3A%2F%2Fgoogle.com", - # "validator.html") - - #var r = get("http://validator.w3.org/check?uri=http%3A%2F%2Fgoogle.com& - # charset=%28detect+automatically%29&doctype=Inline&group=0") - - var data = newMultipartData() - data["output"] = "soap12" - data["uploaded_file"] = ("test.html", "text/html", - "<html><head></head><body><p>test</p></body></html>") - - echo postContent("http://validator.w3.org/check", multipart=data)]# |