-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to get response headers only, without the body? #262
Comments
Here i want to show how to obtain import chronos/apps/http/httpclient
proc getClient() {.async.} =
var session = HttpSessionRef.new({HttpClientFlag.NoVerifyHost,
HttpClientFlag.NoVerifyServerName},
maxRedirections = 10)
var request =
block:
let res = HttpClientRequestRef.new(session, "https://httpbin.org/get",
meth = MethodGet)
if res.isErr():
echo "ERROR: ", res.error()
quit(1)
res.get()
# Here we obtain `response` object.
var response = await request.send()
echo "HTTP RESPONSE STATUS CODE = ", response.status
echo "HTTP RESPONSE HEADES: "
echo $response.headers
await response.closeWait()
await request.closeWait()
await session.closeWait()
proc postClient() {.async.} =
var session = HttpSessionRef.new({HttpClientFlag.NoVerifyHost,
HttpClientFlag.NoVerifyServerName},
maxRedirections = 10)
var request =
block:
let res = HttpClientRequestRef.new(session, "https://httpbin.org/post",
meth = MethodPost)
if res.isErr():
echo "ERROR: ", res.error()
quit(1)
res.get()
request.headers.set("Content-type", "application/json")
request.headers.set("Accept", "application/json")
request.headers.set("Transfer-encoding", "chunked")
var writer = await request.open()
await writer.write("{\"data\": \"data\"}")
await writer.finish()
await writer.closeWait()
# Here we obtain `response` object.
var response = await request.finish()
echo "HTTP RESPONSE STATUS CODE = ", response.status
echo "HTTP RESPONSE HEADES: "
echo $response.headers
await response.closeWait()
await request.closeWait()
await session.closeWait()
when isMainModule:
waitFor getClient()
waitFor postClient() As you can see there no need to dive deep into chronos internals. |
And this is main difference between |
@cheatfate how about we create an |
My Use Case
I needed to calculate the storage requirements for a very large set of archives hosted on a website.
The archives are retrieved with a POST method, so I can't use HEAD.
My Solution
Make the POST call and close the connection after the response headers are received.
I didn't see a means to accomplish this with std/httpclient or chronos/apps/http/httpclient. So this is what I did:
This probably isn't a common use case and worth adding to the implementation, so I'm just dropping it here in case someone finds it useful.
The text was updated successfully, but these errors were encountered: