Handle redirects in HttpClient's post procs & post test.
This commit is contained in:
parent
547fb7f1e4
commit
fa9ec7a6b5
2 changed files with 27 additions and 18 deletions
|
|
@ -964,6 +964,8 @@ proc get*(client: HttpClient | AsyncHttpClient,
|
||||||
## This procedure will follow redirects up to a maximum number of redirects
|
## This procedure will follow redirects up to a maximum number of redirects
|
||||||
## specified in ``client.maxRedirects``.
|
## specified in ``client.maxRedirects``.
|
||||||
result = await client.request(url, HttpGET)
|
result = await client.request(url, HttpGET)
|
||||||
|
|
||||||
|
# Handle redirects.
|
||||||
var lastURL = url
|
var lastURL = url
|
||||||
for i in 1..client.maxRedirects:
|
for i in 1..client.maxRedirects:
|
||||||
if result.status.redirection():
|
if result.status.redirection():
|
||||||
|
|
@ -990,3 +992,11 @@ proc post*(client: HttpClient | AsyncHttpClient, url: string, body = "",
|
||||||
client.headers["Content-Length"] = $len(xb)
|
client.headers["Content-Length"] = $len(xb)
|
||||||
|
|
||||||
result = await client.request(url, HttpPOST, 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
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,15 @@ proc asyncTest() {.async.} =
|
||||||
|
|
||||||
resp = await client.request("https://google.com/")
|
resp = await client.request("https://google.com/")
|
||||||
doAssert(resp.code.is2xx or resp.code.is3xx)
|
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()
|
client.close()
|
||||||
|
|
||||||
# Proxy test
|
# Proxy test
|
||||||
|
|
@ -41,6 +50,14 @@ proc syncTest() =
|
||||||
resp = client.request("https://google.com/")
|
resp = client.request("https://google.com/")
|
||||||
doAssert(resp.code.is2xx or resp.code.is3xx)
|
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()
|
client.close()
|
||||||
|
|
||||||
# Timeout test.
|
# Timeout test.
|
||||||
|
|
@ -56,21 +73,3 @@ proc syncTest() =
|
||||||
syncTest()
|
syncTest()
|
||||||
|
|
||||||
waitFor(asyncTest())
|
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)]#
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue