Convert curl to OCaml

Convert curl commands to OCaml code using Cohttp. curl2code runs entirely in your browser via WebAssembly — your data stays private. For Rust, try our curl to Rust converter. For Elixir, see curl to Elixir. Below are ready-to-use OCaml examples.

How to copy curl from your browser

  1. 1

    Open DevTools

    Press F12 or Ctrl+Shift+I to open your browser developer tools.

  2. 2

    Go to the Network tab

    Click the Network tab and perform the action that triggers the HTTP request.

  3. 3

    Copy as cURL

    Right-click the request → CopyCopy as cURL. Then paste it above.

Frequently Asked Questions

What is OCaml Cohttp?

Cohttp is the standard HTTP client and server library for OCaml. It supports both Lwt (lightweight threads) and Async backends for concurrent I/O. Install with opam install cohttp-lwt-unix. It provides a type-safe, functional API for HTTP requests. curl2code uses Cohttp for all OCaml conversions.

Cohttp vs ocurl vs piaf — which to use?

Cohttp is the standard OCaml HTTP library with Lwt/Async support. ocurl wraps libcurl, giving access to all curl features from OCaml. piaf is newer, HTTP/2-native. Use Cohttp for idiomatic OCaml, ocurl when you need curl-specific features. For similar functional languages, see curl to Elixir or curl to Clojure.

How to handle authentication in OCaml?

Add an Authorization header to the request: Header.add headers "Authorization" ("Bearer " ^ token). For Basic auth, base64-encode credentials using the base64 package. Cohttp headers are immutable — build them with Header.of_list or chain Header.add calls.

How to send multipart form data in OCaml?

Construct the multipart body manually with boundary strings, or use a library like multipart_form. Set the Content-Type header with the boundary. Cohttp's body is a simple string or stream — format the multipart payload according to RFC 2046. curl2code handles -F flag conversion.

How to handle errors in OCaml HTTP requests?

Cohttp with Lwt returns promises — use Lwt.catch or try%lwt ... with syntax for error handling. Check the response status code with Response.status response. OCaml's type system ensures you handle failure cases. Use pattern matching on status codes for exhaustive handling.

How to make concurrent HTTP requests in OCaml?

With Lwt backend: use Lwt.all [request1; request2] for parallel execution or Lwt_list.map_p for mapping over URLs. With Async: use Deferred.all. OCaml's cooperative concurrency through Lwt or Async provides safe, efficient concurrent I/O without thread safety concerns.

How to set timeouts in OCaml?

With Lwt, use Lwt.pick [request; Lwt_unix.sleep timeout >>= fun () -> Lwt.fail Timeout] to race a request against a timer. Cohttp doesn't have built-in timeout options — wrap the Lwt promise with a timeout combinator. For retries, implement a recursive function with decreasing attempts.

How to use a proxy in OCaml?

Cohttp doesn't have built-in proxy support. Use ocurl (libcurl bindings) for proxy needs: set CURLOPT_PROXY option. Alternatively, set HTTP_PROXY environment variable and configure the connection to route through the proxy host and port manually.

How to send a POST request with JSON body in OCaml?

Convert a curl POST command like curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' URL to OCaml using curl2code. The generated code uses Client.post ~body:(Cohttp_lwt.Body.of_string json) uri. curl2code automatically detects -d/--data flags with JSON content and sets the appropriate Content-Type header in the Cohttp code.

How to add Bearer token authorization in OCaml?

Pass curl -H "Authorization: Bearer YOUR_TOKEN" URL to curl2code and it generates Cohttp code with the auth header: Header.add headers 'Authorization' 'Bearer YOUR_TOKEN'. curl2code detects Bearer tokens from both -H "Authorization: Bearer ..." and --oauth2-bearer flags. For Basic auth, use -u user:pass.

How to set Content-Type header in OCaml?

curl2code converts curl -H "Content-Type: application/json" URL to Cohttp code with the proper header: Header.add headers 'Content-Type' 'application/json'. For -d data, curl defaults to application/x-www-form-urlencoded; for -F form uploads, it uses multipart/form-data. curl2code maps each to the correct Cohttp API.

Useful Links

curl Guides

Convert curl to Other Languages