An HTTP request/response parser and serializer for Carp.
(load "git@github.com:carpentry-org/http@0.2.0")(match (Request.parse "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
(Result.Success req)
(println* (Request.verb &req) " " &(URI.str (Request.uri &req)))
(Result.Error e) (IO.errorln &e))(match (Response.parse "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello")
(Result.Success resp)
(println* (Response.code &resp) " " (Response.body &resp))
(Result.Error e) (IO.errorln &e))(let [req (Request.get (URI.zero) [] {} @"")]
(println* &(Request.str &req)))(match (Request.header &req "content-type")
(Maybe.Just ct) (println* "Content-Type: " &ct)
(Maybe.Nothing) (println* "no content-type"))When a message uses Transfer-Encoding: chunked, the parsed body holds the raw
chunk framing. Detect it with chunked? and decode it with
TransferEncoding.dechunk:
(match (Response.parse resp)
(Result.Success r)
(if (Response.chunked? &r)
(match (TransferEncoding.dechunk (Response.body &r))
(Result.Success body) (println* &body)
(Result.Error e) (IO.errorln &e))
(println* (Response.body &r)))
(Result.Error e) (IO.errorln &e))(match (Form.parse "name=carp&version=1")
(Result.Success m)
(println* "name=" &(Map.get &m "name"))
_ ())Status.ok ; => 200
Status.not-found ; => 404
(Status.reason 200) ; => "OK"| Type | Purpose |
|---|---|
Request |
HTTP request (verb, version, URI, headers, cookies, body) |
Response |
HTTP response (code, message, version, headers, cookies, body) |
Cookie |
HTTP cookie with attributes |
SameSite |
Cookie SameSite attribute (Lax, Strict, None) |
Status |
Status code constants and reason phrases |
Form |
URL-encoded form body parser |
MediaType |
Content-Type / media-type parser (type, subtype, parameters) |
Multipart |
multipart/form-data body decoder |
FormPart |
a single decoded multipart part (name, filename, content-type, body) |
TransferEncoding |
Chunked transfer-encoding decoder |
carp -x test/http.carp
You can find the API documentation online!
Have fun!