leanCurl is a Lean 4 package for the libcurl Easy interface.
Status:
Installation steps:
Lake configuration arguments:
To complile on macOS, you have to reinstall curl (linking to the pre-installed libcurl library doesn't work)
brew reinstall curl
Post Json data:
def main : IO Unit := do
-- make IO.Ref Stream.Buffer for response data
let response ← IO.mkRef { : IO.FS.Stream.Buffer}
-- perfom the network transfer
curl_easy_perform_with_options #[
CurlOption.URL "https://dummyjson.com/products/add", -- set Url
CurlOption.COPYPOSTFIELDS "{\"title\": \"curl\"}", -- set HTTP POST data
CurlOption.HTTPHEADER #["Content-Type: application/json", "Accept: application/json"], -- set HttpHeader
CurlOption.WRITEDATA response, -- response data should be written to this buffer
CurlOption.WRITEFUNCTION Curl.writeBytes
]
-- get Stream.Buffer of IO.Ref
let bytes ← response.get
IO.println s!"response: {String.fromUTF8! bytes.data}"
The corresponding curl command is
curl -d "{\"title\": \"curl\"}" https://dummyjson.com/products/add
The --libcurl option displays the correspondence.
Example projects (see build-examples.sh):