Giter Club home page Giter Club logo

curlconverter's Introduction

Transpile curl commands into Python, JavaScript, Java, C#, PHP, Go, Dart, R, Ruby, Rust, MATLAB, Elixir, CFML, Ansible or JSON.

Try it on curlconverter.com or from the command line as a drop-in replacement for curl:

$ curlconverter --data "hello=world" example.com
import requests

data = {
    'hello': 'world',
}

response = requests.post('http://example.com', data=data)

Features:

  • Knows about all 250 of curl's arguments, as well as the deleted ones, but most are ignored
  • Implements a lot of curl's argument parsing logic
    • Supports shortening -O -v -X POST to -OvXPOST
    • --data @filename generates code that reads that file
    • --data @- generates code that reads from stdin (or the piped file/input)
  • Understands most Bash syntax
    • ANSI-C quoted strings (which "Copy as cURL" can output)
    • Piped file or input (such as heredocs)
    • Generates code that gets 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 simpler. 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. For example, in a command like curl example.com?foo=bar&baz=$VAR, if $VAR contains = or & characters or percent encoded characters, that could make the generated code wrong. 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
  • 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

curlconverter requires Node 14+.

Usage

Usage from the command line

The command line tool acts as a drop-in replacement for curl. Take a curl command, change "curl" to "curlconverter" and it will print code instead of making the request

$ curlconverter example.com
import requests

response = requests.get('http://example.com')

or pass - to read the curl command from stdin

$ echo 'curl example.com' | curlconverter -
import requests

response = requests.get('http://example.com')

Choose the output language by passing --language <language>. The options are

  • ansible
  • cfml
  • csharp
  • dart
  • elixir
  • go
  • java
  • javascript, node, node-axios, node-got, node-request
  • json
  • matlab
  • php, php-request
  • python (the default)
  • r
  • ruby
  • rust

--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.

There's a corresponding set of functions that also return an array of warnings if there are any issues with the conversion:

curlconverter.toPythonWarn('curl ftp://example.com');
curlconverter.toPythonWarn(['curl', 'ftp://example.com']);
// [
//   "import requests\n\nresponse = requests.get('ftp://example.com')\n",
//   [ [ 'bad-scheme', 'Protocol "ftp" not supported' ] ]
// ]

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.

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 has to use an old version of curlconverter, so it doesn't support the same languages, curl arguments or Bash syntax as the current version.

Contributing

See CONTRIBUTING.md

License

MIT © Nick Carneiro

curlconverter's People

Contributors

nickcarneiro avatar verhovsky avatar cf512 avatar csells avatar jgroom33 avatar robertof avatar clintonc avatar eliask avatar romadrutskii avatar dainisgorbunovs avatar jay9596 avatar dependabot[bot] avatar yanshiyason avatar trdarr avatar ssi-anik avatar bfontaine avatar seadog007 avatar iamdual avatar markreeder avatar tennyzhuang avatar jeayu avatar hrbrmstr avatar wkalt avatar r3m0t avatar scottsteinbeck avatar codenoid avatar noahcardoza avatar nico202 avatar muzavan avatar mariownyou avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.