Convert curl to R
curl2code converts curl commands to R code using the httr package. The conversion runs in your browser via WebAssembly for complete privacy. For Python, check our curl to Python converter. For Julia, see curl to Julia. Below are practical R examples.
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 R httr?
httr is the most popular HTTP client package for R, designed for web API interactions. It provides functions like GET(), POST(), PUT() with automatic content type handling and OAuth support. Install with install.packages("httr"). curl2code also supports httr2, the next-generation successor.
httr vs httr2 vs curl package — which to use?
httr is stable and widely used — most R tutorials use it. httr2 is the modern replacement with piped syntax, built-in retries, and rate limiting. curl package is a low-level libcurl wrapper. Use httr2 for new projects, httr for compatibility. For similar data science workflows, see curl to Python or curl to Julia.
How to handle authentication in R?
For Bearer: GET(url, add_headers(Authorization = paste("Bearer", token))). For Basic auth: GET(url, authenticate(user, pass)). httr also supports OAuth 1.0 and 2.0 via oauth1.0_token() and oauth2.0_token(). curl2code converts -u and header flags to R syntax.
How to send multipart form data in R?
Use POST(url, body = list(file = upload_file("doc.pdf"), desc = "My file"), encode = "multipart"). httr handles multipart boundaries and Content-Type automatically. For JSON bodies, use encode = "json". curl2code converts -F flags to the httr multipart syntax.
How to handle HTTP errors in R?
Use stop_for_status(response) to throw on non-2xx, warn_for_status() for warnings, or message_for_status() for messages. Check status with status_code(response). Wrap in tryCatch() for programmatic error handling. httr2 uses req_error() for configurable error behavior.
How to parse JSON responses in R?
Use content(response, "parsed") for automatic JSON-to-list parsing, or content(response, "text") with jsonlite::fromJSON() for more control. jsonlite converts JSON to R data frames, lists, and vectors. For data analysis pipelines, pipe directly into tibble or dplyr.
How to set timeouts in R?
Use GET(url, timeout(30)) for a 30-second timeout. httr2 uses req_timeout(req, 30). For retries in httr2: req_retry(req, max_tries = 3, backoff = ~ 2). httr has no built-in retry — use a loop with tryCatch(). These map to curl's --max-time option.
How to use a proxy in R?
Use GET(url, use_proxy("proxy", 8080, username, password)). httr wraps libcurl's proxy options. Also respects http_proxy and https_proxy environment variables. For SOCKS proxies, specify the scheme: use_proxy("socks5://proxy:1080"). curl2code converts -x flags to R proxy config.
How to send a POST request with JSON body in R?
Convert a curl POST command like curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' URL to R using curl2code. The generated code uses POST(url, body = data, encode = 'json'). curl2code automatically detects -d/--data flags with JSON content and sets the appropriate Content-Type header in the httr code.
How to add Bearer token authorization in R?
Pass curl -H "Authorization: Bearer YOUR_TOKEN" URL to curl2code and it generates httr code with the auth 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 R?
curl2code converts curl -H "Content-Type: application/json" URL to httr code with the proper header: content_type_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 httr API.