Convert curl to C
curl2code converts curl commands to C code using libcurl. The conversion runs in your browser via WebAssembly for complete privacy. For Rust, check our curl to Rust converter. For Go, see curl to Go. Below are practical C examples with proper memory management.
How to copy curl from your browser
- 1
Open DevTools
Press F12 or Ctrl+Shift+I to open your browser developer tools.
- 2
Go to the Network tab
Click the Network tab and perform the action that triggers the HTTP request.
- 3
Copy as cURL
Right-click the request → Copy → Copy as cURL. Then paste it above.
Frequently Asked Questions
What is libcurl?
libcurl is the C library behind the curl command-line tool. It's the most portable and widely deployed HTTP client library in existence, supporting 25+ protocols. Used by thousands of applications and embedded systems. Add with your system package manager or link statically. curl2code generates libcurl C code using the easy interface.
libcurl easy vs multi interface — which to use?
The easy interface (curl_easy_*) is for single synchronous transfers — simple and straightforward. The multi interface (curl_multi_*) enables non-blocking concurrent transfers in a single thread. Use easy for scripts and simple tools, multi for high-performance applications. For a higher-level C-family language, see curl to Rust.
How to handle authentication with libcurl?
For Bearer: set a custom header with curl_slist_append(headers, "Authorization: Bearer token"). For Basic auth: curl_easy_setopt(curl, CURLOPT_USERPWD, "user:pass"). libcurl automatically encodes Basic auth credentials. curl2code converts -u and header flags to the correct C API calls.
How to send multipart form data with libcurl?
Use the MIME API (libcurl 7.56+): curl_mime_init() to create a mime structure, curl_mime_addpart() for each field, curl_mime_name() and curl_mime_filedata() for file uploads. Set with CURLOPT_MIMEPOST. Free with curl_mime_free().
How to handle errors with libcurl?
Every curl_easy_* function returns a CURLcode — check against CURLE_OK. Use curl_easy_strerror(code) for human-readable messages. Set CURLOPT_ERRORBUFFER for detailed error strings. Get the HTTP status with curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code).
How to manage memory with libcurl?
Always call curl_easy_cleanup() when done with a handle. Free header lists with curl_slist_free_all(). Free MIME structures with curl_mime_free(). For write callbacks, manage your own buffer allocation. Call curl_global_cleanup() once at program exit. Failing to free resources causes memory leaks.
How to set timeouts with libcurl?
Set CURLOPT_CONNECTTIMEOUT for connection timeout and CURLOPT_TIMEOUT for total transfer time (both in seconds). For millisecond precision, use CURLOPT_CONNECTTIMEOUT_MS and CURLOPT_TIMEOUT_MS. These are the exact same options curl CLI uses with --connect-timeout and --max-time.
How to configure a proxy with libcurl?
Set CURLOPT_PROXY to the proxy URL: curl_easy_setopt(curl, CURLOPT_PROXY, "http://proxy:8080"). For SOCKS5: set CURLOPT_PROXYTYPE to CURLPROXY_SOCKS5. For proxy auth: CURLOPT_PROXYUSERPWD. libcurl also respects the http_proxy environment variable.
How to send a POST request with JSON body in C?
Convert a curl POST command like curl -X POST -H "Content-Type: application/json" -d "{"key":"value"}" URL to C using curl2code. The generated code uses curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_string). curl2code automatically detects -d/--data flags with JSON content and sets the appropriate Content-Type header in the libcurl code.
How to add Bearer token authorization in C?
Pass curl -H "Authorization: Bearer YOUR_TOKEN" URL to curl2code and it generates libcurl code with the auth header: struct curl_slist *headers = curl_slist_append(NULL, "Authorization: Bearer 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 C?
curl2code converts curl -H "Content-Type: application/json" URL to libcurl code with the proper header: curl_slist_append(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 libcurl API.