Skip to content

Commit

Permalink
Merge pull request treeform#101 from treeform/guzba
Browse files Browse the repository at this point in the history
add head request
  • Loading branch information
treeform authored May 17, 2023
2 parents 8ccf307 + 8063d77 commit 1fd7202
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/puppy.nim
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ proc delete*(
): Response =
fetch(newRequest(url, "DELETE", headers, timeout))

proc head*(
url: string,
headers = emptyHttpHeaders(),
timeout: float32 = 60
): Response =
fetch(newRequest(url, "HEAD", headers, timeout))

proc fetch*(url: string, headers = emptyHttpHeaders()): string =
## Simple fetch that directly returns the GET response body.
## Raises an exception if anything goes wrong or if the response code
Expand Down
4 changes: 3 additions & 1 deletion src/puppy/platforms/linux/platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ proc internalFetch*(req: Request): Response {.raises: [PuppyError].} =

discard curl.easy_setopt(OPT_HTTPHEADER, headerList)

if req.verb.toUpperAscii() == "POST" or req.body.len > 0:
if req.verb.toUpperAscii() == "HEAD":
discard curl.easy_setopt(OPT_NOBODY, 1)
elif req.verb.toUpperAscii() == "POST" or req.body.len > 0:
discard curl.easy_setopt(OPT_POSTFIELDSIZE, req.body.len)
discard curl.easy_setopt(OPT_POSTFIELDS, req.body.cstring)

Expand Down
5 changes: 5 additions & 0 deletions tests/test.nim
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ block:
)
)

block:
let response = head("https://www.google.com")
doAssert response.code == 200
doAssert response.body.len == 0

when defined(windows):
block:
let httpsServer = startProcess(
Expand Down

0 comments on commit 1fd7202

Please sign in to comment.