Skip to content
koducathome

cURL to OCaml Converter

Convert curl commands to OCaml code using cohttp. Paste a command and the OCaml snippet appears instantly — everything runs in your browser.

How to use this tool

Steps for using the cURL to Code.

  1. Copy a curl command — from API documentation, or from your browser's DevTools (Network tab → right-click a request → Copy as cURL (bash)).
  2. Paste it into Enter cURL command. The OCaml cohttp code appears instantly.
  3. Set up: Install with opam install cohttp-lwt-unix; for HTTPS also install a TLS backend such as tls-lwt or lwt_ssl.
  4. Run it: Build with dune (libraries cohttp-lwt-unix) and run the executable. Replace placeholders such as YOUR_TOKEN first, and check any notes under the editors for curl options that have no cohttp equivalent.

Example

GET with headers → OCaml cohttp

curl command
curl 'https://api.example.com/v1/users?page=2' \
  -H 'Accept: application/json' \
  -H 'Authorization: Bearer YOUR_TOKEN'
OCaml cohttp
open Lwt.Infix
open Cohttp
open Cohttp_lwt_unix

let request =
  let uri = Uri.of_string "https://api.example.com/v1/users?page=2" in
  let headers =
    Header.of_list
      [
        ("Accept", "application/json");
        ("Authorization", "Bearer YOUR_TOKEN");
      ]
  in
  Client.call ~headers `GET uri >>= fun (resp, resp_body) ->
  Cohttp_lwt.Body.to_string resp_body >|= fun text ->
  Printf.printf "%d\n" (Code.code_of_status (Response.status resp));
  print_endline text

let () = Lwt_main.run request

Common use cases

  • OCaml services and tools

Converting curl to OCaml cohttp

cohttp with Lwt is a common OCaml HTTP client.

The converter reads the curl command the way curl does — the method (-X), headers (-H), request bodies (-d, --data-raw, --json, -F), query strings, basic auth (-u) and cookies — and writes the equivalent cohttp code. Options that have no cohttp equivalent are listed under the editors rather than silently dropped.

Limitations

  • The code is a starting point: add timeouts, retries and error handling for production use.
  • Secrets in the curl command are copied into the code — move them to environment variables before committing.

Frequently asked questions

Why does HTTPS fail with cohttp?

cohttp needs a TLS library for HTTPS; install tls-lwt or lwt_ssl.

What is Lwt_main.run?

It runs the Lwt promise for the request until it completes.

Is my curl command sent to a server?

No. The conversion runs entirely in your browser, so tokens and passwords in the command never leave your device. Still, replace real secrets before sharing the generated code.