Skip to content

Commit

Permalink
payload: add buffered-payload
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdanp committed Oct 31, 2024
1 parent 0b2d5ed commit 7b769fc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
17 changes: 17 additions & 0 deletions http-easy-lib/http-easy/private/payload.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,28 @@

(provide
(contract-out
[buffered-payload (-> payload-procedure/c payload-procedure/c)]
[form-payload (-> form-data/c payload-procedure/c)]
[gzip-payload (-> payload-procedure/c payload-procedure/c)]
[json-payload (-> jsexpr? payload-procedure/c)]
[pure-payload (-> (or/c bytes? string? input-port?) payload-procedure/c)]))

(define ((buffered-payload p) hs)
(let*-values ([(hs data) (p hs)]
[(bs)
(cond
[(input-port? data)
(call-with-output-bytes
(lambda (out)
(copy-port data out)))]
[(string? data)
(string->bytes/utf-8 data)]
[else
data])])
(define content-length
(number->string (bytes-length bs)))
(values (hash-set hs 'content-length content-length) bs)))

(define (form-payload v)
(define data (alist->form-urlencoded v))
(lambda (hs)
Expand Down
2 changes: 1 addition & 1 deletion http-easy-lib/info.rkt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#lang info

(define license 'BSD-3-Clause)
(define version "0.7")
(define version "0.8")
(define collection "net")
(define deps
'(["base" #:version "8.1.0.4"]
Expand Down
8 changes: 8 additions & 0 deletions http-easy/http-easy.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,14 @@ sent to a remote server.
value to be used as the request body.
}

@defproc[(buffered-payload [p payload-procedure/c]) payload-procedure/c]{
Produces a payload procedure that buffers the output of @racket[p]
in memory in order to determine its length before sending it to the
server.

@history[#:added "0.8"]
}

@defproc[(form-payload [v form-data/c]) payload-procedure/c]{
Produces a payload procedure that encodes @racket[v] as form data
using the @tt{application/x-www-form-urlencoded} content type.
Expand Down

0 comments on commit 7b769fc

Please sign in to comment.