http
General purpose data, functions, and utilities for use by LFE HTTP clients, servers, URL-parsers, web frameworks, etc.
Table of Contents
Features ↟
- Binary-first design: All strings are binaries by default for optimal performance
- Fluent builder pattern: Chain operations for readable code
- Request maps:
(http.request:new #"GET" "http://example.com") - Response helpers:
(http.response:ok #"Hello, World!"),(http.response:json 200 data) - Smart headers: Case-insensitive lookups, single-pass conversions
- Erlang httpc interop:
(http.c:request "http://google.com") - 150+ status codes:
(http.status:im-a-teapot),(http.status:code->message 200) - 2000+ MIME types:
(http.mimetype:from-extension "json") - Query parameters: Automatic parsing and reconstruction
- Content helpers:
set-json,set-form,set-textfor common formats
Examples ↟
Basic HTTP Client Requests
Start the required applications:
(inets:start)
(ssl:start)Simple GET request:
> (http.c:request "https://httpbin.org/get")
#(ok #m(status 200
headers #m(#"Content-Type" #"application/json" ...)
body #"..."
version 1.1))GET request with custom headers:
> (let* ((req (http.request:new "https://httpbin.org/headers"))
(req-with-headers (http.request:set-header req #"User-Agent" #"lfe-http/1.0")))
(http.c:request req-with-headers))
#(ok #m(status 200 ...))POST request with JSON:
> (let* ((body #"{\"name\":\"LFE\",\"type\":\"language\"}")
(req (http.request:new #"POST" "https://httpbin.org/post"))
(req-with-json (http.request:set-json req body)))
(http.c:request req-with-json))
#(ok #m(status 200 ...))POST with custom headers and body:
> (let* ((headers (http.header:add (http.header:new)
#"Content-Type"
#"application/x-www-form-urlencoded"))
(body #"key1=value1&key2=value2"))
(http.c:request #"POST" "https://httpbin.org/post" body headers))
#(ok #m(status 200 ...))Working with Headers
Create a new headers map:
> (http.header:new)
#m()Add headers:
> (let* ((h1 (http.header:new))
(h2 (http.header:add h1 #"Content-Type" #"application/json"))
(h3 (http.header:add h2 #"Accept" #"application/json")))
h3)
#m(#"Content-Type" #"application/json"
#"Accept" #"application/json")Convert from a property list:
> (http.header:from-list '(#(#"Content-Type" #"text/plain")
#(#"Accept" #"*/*")))
#m(#"Content-Type" #"text/plain"
#"Accept" #"*/*")Get header values (case-sensitive):
> (let ((headers (http.header:from-list '(#(#"Content-Type" #"application/json")))))
(http.header:get headers #"Content-Type"))
#"application/json"Get header values (case-insensitive):
> (let ((headers (http.header:from-list '(#(#"Content-Type" #"application/json")))))
(http.header:get headers #"content-type" 'undefined #m(case-insensitive true)))
#"application/json"Merge headers:
> (let ((h1 (http.header:from-list '(#(#"Accept" #"application/json"))))
(h2 (http.header:from-list '(#(#"Content-Type" #"text/plain")))))
(http.header:merge h1 h2))
#m(#"Accept" #"application/json"
#"Content-Type" #"text/plain")Installation ↟
Add it to your rebar.config deps:
{deps, [
...
{http, "1.0.0", {pkg, lfe_http}}
]}.License ↟
Apache License, Version 2.0
Copyright © 2023-2025, Duncan McGreggor oubiwann@gmail.com.