Generated code reads environment variables and runs subcommands
Ignores comments
Reports syntax errors
Converts JSON data to native objects
Warns about issues with the conversion
Limitations:
Only HTTP is supported
Code generators for other languages are less thorough than the Python generator
curl doesn’t follow redirects or decompress gzip-compressed responses by default, but the generated code will do whatever the default is for that runtime, to keep it shorter. For example Python’s Requests library follows redirects by default, so unless you explicitly set the redirect policy with -L/--location/--no-location, the generated code will not handle redirects the same way as the curl command
Shell variables can arbitrarily change how the command would be parsed at runtime. The command curl $VAR can do anything, depending on what’s in $VAR. curlconverter assumes that environment variables don’t contain characters that would affect parsing
Only simple subcommands such as curl $(echo example.com) work, more complicated subcommands (such as nested commands or subcommands that redirect the output) won’t generate valid code
The Bash parser doesn’t support all Bash syntax
and much more
Install
Install the command line tool with
npm install --global curlconverter
Install the JavaScript library for use in your own projects with
npm install curlconverter
Usage
Usage from the command line
curlconverter acts as a drop-in replacement for curl. Take any curl command, change “curl“ to “curlconverter“ and it will print code instead of making the request
--verbose enables printing of conversion warnings and error tracebacks.
Usage as a library
The JavaScript API is a bunch of functions that can take either a string of Bash code or an array of already-parsed arguments (like process.argv) and return a string with the resulting program:
import * as curlconverter from 'curlconverter';
curlconverter.toPython('curl example.com');
curlconverter.toPython(['curl', 'example.com']);
// "import requests\n\nresponse = requests.get('http://example.com')\n"
Note: add "type": "module", to your package.json for the import statement above to work. curlconverter must be imported as an ES module with import this way and not with require() because it uses top-level await.
There’s a corresponding set of functions that also return an array of warnings if there are any issues with the conversion:
If you want to host curlconverter yourself and use it in the browser, it needs two WASM files to work, tree-sitter.wasm and tree-sitter-bash.wasm, which it will request from the root directory of your web server. If you are hosting a static website and using Webpack, you need to copy these files from the node_modules/ directory to your server’s root directory in order to serve them. You can look at the webpack.config.js for curlconverter.com to see how this is done. You will also need to set {module: {experiments: {topLevelAwait: true}}} in your webpack.config.js.
curlconverter
Transpile
curlcommands into C, C#, ColdFusion, Clojure, Dart, Elixir, Go, HTTPie, Java, JavaScript, Julia, Kotlin, Lua, MATLAB, Objective-C, OCaml, Perl, PHP, PowerShell, Python, R, Ruby, Rust, Swift, Wget, Ansible, HAR, HTTP or JSON.Try it on curlconverter.com or as a drop-in
curlreplacement:Features:
-O -v -X POSTto-OvXPOST--data @filenamegenerates code that reads that file and@-reads stdinLimitations:
-L/--location/--no-location, the generated code will not handle redirects the same way as the curl commandcurl $VARcan do anything, depending on what’s in$VAR. curlconverter assumes that environment variables don’t contain characters that would affect parsingcurl $(echo example.com)work, more complicated subcommands (such as nested commands or subcommands that redirect the output) won’t generate valid codeInstall
Install the command line tool with
Install the JavaScript library for use in your own projects with
Usage
Usage from the command line
curlconverteracts as a drop-in replacement for curl. Take any curl command, change “curl“ to “curlconverter“ and it will print code instead of making the requestTo read the curl command from stdin, pass
-Choose the output language by passing
--language <language>. The options areansibleccfmlclojurecsharpdartelixirgoharhttphttpiejava,java-httpurlconnection,java-jsoup,java-okhttpjavascript,javascript-jquery,javascript-xhrjsonjuliakotlinluamatlabnode,node-http,node-axios,node-got,node-ky,node-request,node-superagentobjcocamlperlphp,php-guzzle,php-requestspowershell,powershell-webrequestpython(the default),python-httpr,r-httr2ruby,ruby-httpartyrustswiftwget--verboseenables printing of conversion warnings and error tracebacks.Usage as a library
The JavaScript API is a bunch of functions that can take either a string of Bash code or an array of already-parsed arguments (like
process.argv) and return a string with the resulting program:Note: add
"type": "module",to your package.json for theimportstatement above to work. curlconverter must be imported as an ES module withimportthis way and not withrequire()because it uses top-levelawait.There’s a corresponding set of functions that also return an array of warnings if there are any issues with the conversion:
If you want to host curlconverter yourself and use it in the browser, it needs two WASM files to work,
tree-sitter.wasmandtree-sitter-bash.wasm, which it will request from the root directory of your web server. If you are hosting a static website and using Webpack, you need to copy these files from the node_modules/ directory to your server’s root directory in order to serve them. You can look at the webpack.config.js for curlconverter.com to see how this is done. You will also need to set{module: {experiments: {topLevelAwait: true}}}in your webpack.config.js.Usage in VS Code
There’s a VS Code extension that adds a “Paste cURL as <language>“ option to the right-click menu: https://marketplace.visualstudio.com/items?itemName=curlconverter.curlconverter. It doesn’t support the same languages, curl arguments or Bash syntax as the current version because it has to use an old version of curlconverter.
Contributing
See CONTRIBUTING.md
License
MIT © Nick Carneiro