Implements getContent and postContent for (Async)HttpClient.
This commit is contained in:
parent
fa9ec7a6b5
commit
0baef8e6ea
2 changed files with 55 additions and 1 deletions
|
|
@ -950,10 +950,13 @@ proc request*(client: HttpClient | AsyncHttpClient, url: string,
|
||||||
## Connects to the hostname specified by the URL and performs a request
|
## Connects to the hostname specified by the URL and performs a request
|
||||||
## using the method specified.
|
## using the method specified.
|
||||||
##
|
##
|
||||||
## Connection will kept alive. Further requests on the same ``client`` to
|
## Connection will be kept alive. Further requests on the same ``client`` to
|
||||||
## the same hostname will not require a new connection to be made. The
|
## the same hostname will not require a new connection to be made. The
|
||||||
## connection can be closed by using the ``close`` procedure.
|
## connection can be closed by using the ``close`` procedure.
|
||||||
##
|
##
|
||||||
|
## When a request is made to a different hostname, the current connection will
|
||||||
|
## be closed.
|
||||||
|
##
|
||||||
## The returned future will complete once the request is completed.
|
## The returned future will complete once the request is completed.
|
||||||
result = await request(client, url, $httpMethod, body)
|
result = await request(client, url, $httpMethod, body)
|
||||||
|
|
||||||
|
|
@ -973,6 +976,21 @@ proc get*(client: HttpClient | AsyncHttpClient,
|
||||||
result = await client.request(redirectTo, HttpGET)
|
result = await client.request(redirectTo, HttpGET)
|
||||||
lastURL = redirectTo
|
lastURL = redirectTo
|
||||||
|
|
||||||
|
proc getContent*(client: HttpClient | AsyncHttpClient,
|
||||||
|
url: string): Future[string] {.multisync.} =
|
||||||
|
## Connects to the hostname specified by the URL and performs a GET request.
|
||||||
|
##
|
||||||
|
## This procedure will follow redirects up to a maximum number of redirects
|
||||||
|
## specified in ``client.maxRedirects``.
|
||||||
|
##
|
||||||
|
## A ``HttpRequestError`` will be raised if the server responds with a
|
||||||
|
## client error (status code 4xx) or a server error (status code 5xx).
|
||||||
|
let resp = await get(client, url)
|
||||||
|
if resp.code.is4xx or resp.code.is5xx:
|
||||||
|
raise newException(HttpRequestError, resp.status)
|
||||||
|
else:
|
||||||
|
return resp.body
|
||||||
|
|
||||||
proc post*(client: HttpClient | AsyncHttpClient, url: string, body = "",
|
proc post*(client: HttpClient | AsyncHttpClient, url: string, body = "",
|
||||||
multipart: MultipartData = nil): Future[Response] {.multisync.} =
|
multipart: MultipartData = nil): Future[Response] {.multisync.} =
|
||||||
## Connects to the hostname specified by the URL and performs a POST request.
|
## Connects to the hostname specified by the URL and performs a POST request.
|
||||||
|
|
@ -1000,3 +1018,20 @@ proc post*(client: HttpClient | AsyncHttpClient, url: string, body = "",
|
||||||
var meth = if result.status != "307": HttpGet else: HttpPost
|
var meth = if result.status != "307": HttpGet else: HttpPost
|
||||||
result = await client.request(redirectTo, meth, xb)
|
result = await client.request(redirectTo, meth, xb)
|
||||||
lastURL = redirectTo
|
lastURL = redirectTo
|
||||||
|
|
||||||
|
proc postContent*(client: HttpClient | AsyncHttpClient, url: string,
|
||||||
|
body = "",
|
||||||
|
multipart: MultipartData = nil): Future[string]
|
||||||
|
{.multisync.} =
|
||||||
|
## 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 ``client.maxRedirects``.
|
||||||
|
##
|
||||||
|
## A ``HttpRequestError`` will be raised if the server responds with a
|
||||||
|
## client error (status code 4xx) or a server error (status code 5xx).
|
||||||
|
let resp = await post(client, url, body, multipart)
|
||||||
|
if resp.code.is4xx or resp.code.is5xx:
|
||||||
|
raise newException(HttpRequestError, resp.status)
|
||||||
|
else:
|
||||||
|
return resp.body
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,16 @@ 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)
|
||||||
|
|
||||||
|
# getContent
|
||||||
|
try:
|
||||||
|
discard await client.getContent("https://google.com/404")
|
||||||
|
doAssert(false, "HttpRequestError should have been raised")
|
||||||
|
except HttpRequestError:
|
||||||
|
discard
|
||||||
|
except:
|
||||||
|
doAssert(false, "HttpRequestError should have been raised")
|
||||||
|
|
||||||
|
|
||||||
# Multipart test.
|
# Multipart test.
|
||||||
var data = newMultipartData()
|
var data = newMultipartData()
|
||||||
data["output"] = "soap12"
|
data["output"] = "soap12"
|
||||||
|
|
@ -50,6 +60,15 @@ 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)
|
||||||
|
|
||||||
|
# getContent
|
||||||
|
try:
|
||||||
|
discard client.getContent("https://google.com/404")
|
||||||
|
doAssert(false, "HttpRequestError should have been raised")
|
||||||
|
except HttpRequestError:
|
||||||
|
discard
|
||||||
|
except:
|
||||||
|
doAssert(false, "HttpRequestError should have been raised")
|
||||||
|
|
||||||
# Multipart test.
|
# Multipart test.
|
||||||
var data = newMultipartData()
|
var data = newMultipartData()
|
||||||
data["output"] = "soap12"
|
data["output"] = "soap12"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue