Giter Club home page Giter Club logo

curlconverter's Introduction

Transpile curl commands 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 curl replacement:

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

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

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

Features:

  • Implements a lot of curl's argument parsing logic
    • Knows about all 255 curl arguments but most are ignored
    • Supports shortening -O -v -X POST to -OvXPOST
    • --data @filename generates code that reads that file and @- reads stdin
  • Understands Bash syntax
    • ANSI-C quoted strings
    • stdin redirects and heredocs
    • 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

curlconverter requires Node 12+.

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

$ curlconverter example.com
import requests

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

To read the curl command from stdin, pass -

$ 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
  • c
  • cfml
  • clojure
  • csharp
  • dart
  • elixir
  • go
  • har
  • http
  • httpie
  • java, java-httpurlconnection, java-jsoup, java-okhttp
  • javascript, javascript-jquery, javascript-xhr
  • json
  • julia
  • kotlin
  • lua
  • matlab
  • node, node-http, node-axios, node-got, node-ky, node-request, node-superagent
  • objc
  • ocaml
  • perl
  • php, php-guzzle, php-requests
  • powershell, powershell-webrequest
  • python (the default), python-http
  • r
  • ruby
  • rust
  • swift
  • wget

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

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

curlconverter's People

Contributors

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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

curlconverter's Issues

header fields are ignored

Hi.
Consider the following Curl Command:

curl -X GET --header 'Accept: application/json' --header 'user-token: 75d7ce4350c7d6239347bf23d3a3e668' 'http://localhost:8080/api/retail/books/list'

It gets converted to:

import requests

requests.get('http://localhost:8080/api/retail/books/list')

the "user-token" is ignored.
Instead it should be converted to:

import requests

requests.get('http://localhost:8080/api/retail/books/list',headers={'user-token': '75d7ce4350c7d6239347bf23d3a3e668')

Add python support for OPTIONS request method

Hello,

I am trying to convert his:

curl "https://layla.amazon.de/api/tunein/queue-and-play?deviceSerialNumber=xxx^&deviceType=xxx^&guideId=s56876^&contentType=station^&callSign=^&mediaOwnerCustomerId=xxx" -X OPTIONS -H "Pragma: no-cache" -H "Access-Control-Request-Method: POST" -H "Origin: https://alexa.amazon.de" -H "Accept-Encoding: gzip, deflate, br" -H "Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" -H "Accept: */*" -H "Cache-Control: no-cache" -H "Referer: https://alexa.amazon.de/spa/index.html" -H "Connection: keep-alive" -H "DNT: 1" -H "Access-Control-Request-Headers: content-type,csrf" --compressed

I think, this should be a post request. Nevertheless, it is always converted to a get request.
How can I enforce a post?

Greetings,
Hendrik

Add julia (Requests.jl) generator

Hi, I'm adding the generator for julia.
The Requests.jl package is similar to python requests.
I'm adapting the python generator and most things are working, will pull request when everything is done

Thanks, Nicolò

copy curl request from Charles, url undefine

I use Charles get curl Request, get that:

curl -H "Host: api.ipify.org" -H "Accept: */*" -H "User-Agent: GiftTalk/2.7.2 (iPhone; iOS 9.0.2; Scale/3.00)" -H "Accept-Language: en-CN;q=1, zh-Hans-CN;q=0.9" --compressed http://api.ipify.org/?format=json&

the curl write in terminal works fine, but I wrote into http://curl.trillworks.com/, got

headers = {
    'Host': 'api.ipify.org',
    'Accept': '*/*',
    'User-Agent': 'GiftTalk/2.7.2 (iPhone; iOS 9.0.2; Scale/3.00)',
    'Accept-Language': 'en-CN;q=1, zh-Hans-CN;q=0.9',
}

requests.get('undefined', headers=headers)

--data-binary fails to read file into string

Hi,

I used following CURL command,

curl -i -X POST \ --data-binary @./sample.sparql \ -H "Content-type: application/sparql-query" \ -H "Accept: application/sparql-results+json" \ http://lodstories.isi.edu:3030/american-art/query

which resulted in below code,

`
import requests

headers = {
'Content-type': 'application/sparql-query',
'Accept': 'application/sparql-results+json',
}

data = open('./sample.sparql')
requests.post('http://lodstories.isi.edu:3030/american-art/query', headers=headers, data=data)
`

However, this fails to return any results. I had to replace line data = open('./sample.sparql') with data = open('./sample.sparql', 'rb').read() to get it working.

Parse -XPOST and -XGET commands

Following curl call works for me with curl, but your parser throws an error when it tries to parse it:
curl -XPOST 'http://us.jooble.org/api/xxxxxxxxxxxxxxxx' --data '{"keywords":"php","page":1,"searchMode":1}'

Multiline curl command & -d params

I've tried to convert command below but it didn't work:

curl -X GET 'http://fiddle.jshell.net/echo/html/'\
    -H 'Origin: http://fiddle.jshell.net'\
    -d msg1="value1"\
    -d msg2="value2"

First, since it is multi line command, yargs will throw an error.
Secondly I guess this library only support for --data not -d.

Do not set 'Content-Type' for Python

Setting 'Content-Type', causing problem in Python 'request', if "Content-Type" is set requests is not sending 'boundry' in the header.

This is creating problem in accepting 'multipart-form'.

Parse urls showing up after -s argument

Hello

Here it is

curl -s 'https://cmdb.litop.local/webservices/rest.php' -d 'version=1.2' -d "auth_user=fdgxf" -d "auth_pwd=oxfdscds" -d 'json_data={ "operation": "core/get", "class": "Software", "key": "key" }'

Generate code on paste

Most people paste code. No reason to click the button in most cases. Attempt to autogenerate on every keystroke in the input box.

Add generator for Swift (Alamofire)

Hi, I'm not a Javascript developer, but sometimes I coding small things in python and javascript and this project is fucking awesome.

I'm a iOS developer, and looking this project, I was thinking if is possible generate the request in Swift using (https://github.com/Alamofire/Alamofire) for example.

If you have a start point for me in your code to study and begin with this I appreciate.

(sorry for opening a issue for this, if its not appropriate)

Thanks again and congratulations for this project.

correct curl is not translated to Python code

I get a parsing error with this curl command:

curl "https://www.nadlan.gov.il/KeshetAnnan.REST/Main/GetDataByQuery?query=^%^D7^%^A9^%^D7^%^9B^%^D7^%^95^%^D7^%^A0^%^D7^%^AA^%^20^%^D7^%^A8^%^D7^%^9E^%^D7^%^AA^%^20^%^D7^%^90^%^D7^%^A9^%^D7^%^9B^%^D7^%^95^%^D7^%^9C^%^2C^%^20^%^D7^%^99^%^D7^%^A8^%^D7^%^95^%^D7^%^A9^%^D7^%^9C^%^D7^%^99^%^D7^%^9D^%^20" -H "Accept-Encoding: gzip, deflate, br" -H "Accept-Language: en-US,en;q=0.8" -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" -H "Accept: application/json, text/plain, */*" -H "Referer: https://www.nadlan.gov.il/^%^D7^%^A8^%^D7^%^97^%^D7^%^95^%^D7^%^91^%^20^%^D7^%^99^%^D7^%^9D^%^20^%^D7^%^A1^%^D7^%^95^%^D7^%^A3^%^208,^%^20^%^D7^%^99^%^D7^%^A8^%^D7^%^95^%^D7^%^A9^%^D7^%^9C^%^D7^%^99^%^D7^%^9D^%^20" -H "Cookie: _ga=GA1.3.584052889.1509031917; _gid=GA1.3.1552049848.1509031917; keshet-client-token=eL7onXBKTzKmYReUJ2zri2jG0hm0O7f8I0eNK+2Dzby27SrBUoI7pNz2NbE+gMyeYaLHhH/NBlctm0OyaqgSe2P17fB5BCG97LUfhr/PGZaoOqzBAxtHtWPfGYj1Bp2yGiWCAJQWHNsetxsjkOJ49exg5whBmzwRRe2Z+t7l5ygQaeE2C5easc3PETUgTm5m" -H "Connection: keep-alive" --compressed

Add generator for Java

Java is super popular and super verbose, making it a good candidate for curlconverter.

We need to find out if there is some modern library for sending http requests. Please advise.

I've used the Apache HttpClient before, but this looks interesting: http://unirest.io/java.html
There's no reason we couldn't have two generators.

numbers in --data are not detected

curl -X POST -d '123' http://a.com

to Python requests

import requests

requests.post('http://a.com/')

ignores -d parameter when the data is a numeric string.

Holy shit.

I'm sorry about using issues for this,
But holy shit, thank you so much for creating this.
I was really frustrated with making a post request node.js. But I couldn't get it to work, and I was about to give up completely, but then somehow I stumbled upon your tool, and decided "why not test this?". So I did, and I posted it into my code, and it just magically worked.
Thank you so much.

If I wasn't a broke teenager then I would have bought you a pizza or anything. But I hope this thank you is enough =)

/Erik

Build fails against versions of Node bundled with npm 3

Travis is building (successfully) against Node 0.10, which is bundled with npm 1.

$ nvm use 0.10
Now using node v0.10.41 (npm v1.4.29)

Node 0.12 and 4 are bundled with npm 2, which warns about a change to the way that peer dependencies will be (are) handled in npm 3. The build succeeds against both:

$ nvm use 0.12
Now using node v0.12.9 (npm v2.14.9)
$ npm install && npm test
npm WARN peerDependencies The peer dependency grunt@~0.4.0 included from grunt-contrib-jshint will no
npm WARN peerDependencies longer be automatically installed to fulfill the peerDependency
npm WARN peerDependencies in npm 3+. Your application will need to depend on it explicitly.
npm WARN peerDependencies The peer dependency grunt@~0.4.0 included from grunt-contrib-watch will no
npm WARN peerDependencies longer be automatically installed to fulfill the peerDependency
npm WARN peerDependencies in npm 3+. Your application will need to depend on it explicitly.
…
Done, without errors.
$ nvm use 4
Now using node v4.2.6 (npm v2.14.12)
$ npm install && npm test
npm WARN peerDependencies The peer dependency grunt@~0.4.0 included from grunt-contrib-jshint will no
npm WARN peerDependencies longer be automatically installed to fulfill the peerDependency
npm WARN peerDependencies in npm 3+. Your application will need to depend on it explicitly.
npm WARN peerDependencies The peer dependency grunt@~0.4.0 included from grunt-contrib-watch will no
npm WARN peerDependencies longer be automatically installed to fulfill the peerDependency
npm WARN peerDependencies in npm 3+. Your application will need to depend on it explicitly.
…
Done, without errors.

Building against Node 5, bundled with npm 3, fails:

$ nvm use 5
Now using node v5.5.0 (npm v3.3.12)
$ npm install && npm test
…
npm WARN EPEERINVALID [email protected] requires a peer of grunt@~0.4.0 but none was installed.
npm WARN EPEERINVALID [email protected] requires a peer of grunt@~0.4.0 but none was installed.
…
Fatal error: Unable to find local grunt.
…
npm ERR! Test failed.  See above for more details.

Doesn't support --data-binary?

input

curl 'http://example.com/post' --data-binary '{"title":"china1"}'

result

requests.get('http://example.com/post')

expect

requests.post('http://example.com/post', data={'title':'china1'})

Parse form data as python dict literal when appropriate

Hi

First of all thanks for your useful tool.

I had an issue by parsing the following curl command:

curl -X POST -d "grant_type=client_credentials" -u "foo:bar" http://localhost/api/oauth/token/

Your tool returns this:

data = 'grant_type=client_credentials'
requests.post('http://localhost/api/oauth/token/', data=data)

First, as mentioned in #24, -u option is not processed.
Second, the data field does not work in requests. To make it work, I had to change the first line for data = {'grant_type':'client_credentials'}.
In conclusion, your tool should have return this:

data = {'grant_type': 'client_credentials'}
user = ('foo', 'bar')
requests.post('http://localhost/api/oauth/token/', data=data, auth=user)

Regards,

Clément

Usefull links:

Empty --data or --data-binary results in error

Used by programs such as Charles Proxy and Chrome for a POST with no data.

Example command:
curl --data-binary "" google.com

Console stack trace

TypeError: t.data.startsWith is not a function
Stack trace:
i@https://curl.trillworks.com/scripts/main.js:1:15003
[11]</f@https://curl.trillworks.com/scripts/main.js:1:16207
[1]</a@https://curl.trillworks.com/scripts/main.js:1:2815

Short form of insecure (-k) request not working

The syntax in the following curl command is correct, but the converter won't convert it to python requests.

curl -k 'https://www.site.com'

Great program btw! I'm pretty familiar with curl, but am just learning requests and your converter has helped me learn a lot.

correct curl patch url is not generated by python requests

this is the sample code. which i was trying to convert to python, but is showing up POST instead of PATCH

curl 'https://ci.example.com/go/api/agents/adb9540a-b954-4571-9d9b-2f330739d4da' \
      -u 'username:password' \
      -H 'Accept: application/vnd.go.cd.v4+json' \
      -H 'Content-Type: application/json' \
      -X PATCH \
      -d '{
        "hostname": "agent02.example.com",
        "agent_config_state": "Enabled",
        "resources": ["Java","Linux"],
        "environments": ["Dev"]
        }'

--data-binary not correctly handled in nodejs

while the code in python is properly generated in node the following input

curl -X POST "https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?language=es-ES&locale=es-ES&format=simple&requestid=req_id" -H 'Authorization: Bearer myToken' -H 'Transfer-Encoding: chunked'  -H 'Content-type: audio/wav; codec="audio/pcm"; samplerate=8000' --data-binary @"RE659c5bc74e712750fa7a66e1caad4fde.wav"

does not recognize --data-binary @"RE659c5bc74e712750fa7a66e1caad4fde.wav" like it should open the file

adding something like this should fix the issue ;)

var fs = require('fs');

var data = fs.readFileSync('./RE659c5bc74e712750fa7a66e1caad4fde.wav');

binary data argument not reflected in requests output

Hey there,

Really useful tool! Thank you very much. I got thrown for a bit of a loop when uploading a file to an AWS S3 (nice work on parsing the auth url BTW). The curl had the argument --data-binary however the requests output still suggested using requests.put(url, data=data). The solution to this seems to be

with open(file, 'rb') as data:
      requests.put(url, data=data)

(https://stackoverflow.com/questions/31341808/how-to-do-put-on-amazon-s3-using-python-requests)

Maybe it's obvious that the data needs to be read as binary to more experienced users, but from my perspective: maybe it's worth a comment in the output or using different parsing to draw attention to that?

Would be happy to try and implement that myself if you agree.

Add generator for httpie

httpie is a dev-friendly command-line tool to send requests with parameters, JSON request, custom headers, etc. and see the pretty-printed response.

Parser chokes on quotation marks in --data argument

(I'm basing this report on the behavior of the online demonstration at http://curl.trillworks.com/. My apologies if that demo is simply out of date and this problem is already fixed in the most recent code, or if this issue is an artifact of the web interface.)

It seems that I cannot pass single or double quote characters in the curl command.

For example, given:

curl -X POST http://example.com/ --data "foo=bar"

curlconverter generates the proper body content (taking the Node.js output):

var dataString = 'foo=bar';

The same is true for the single quote version:

curl -X POST http://example.com/ --data 'foo=bar'

However, if I try to add quotation marks in the body content, e.g.:

curl -X POST http://example.com/ --data 'foo="bar"'

The output body is truncated at the first quotation mark:

var dataString = 'foo=';

There are similar problems for all of the variations of this input I could think of, for example:

curl -X POST http://example.com/ --data "foo=\"bar\""
curl -X POST http://example.com/ --data 'foo=\"bar\"'
curl -X POST http://example.com/ --data "foo=\'bar\'"
curl -X POST http://example.com/ --data "foo='bar'"

etc.

Chrome Extension?

Somewhere between a question and suggestion, certainly not a "issue" — could one craft a Chrome Extension to add "Copy as Python Request" directly to Chrome's Network tab options?

I've looked into it briefly and didn't get far, but stirring the pot for anyone who might be more skilled than me :D Also happy to close if this is too far off topic for this project.

Convert curl syntax to Python, Node.js, PHP

I used link https://curl.trillworks.com/#python to convert curl command to python requests syntax. But it show me error "Error parsing curl command".

Syntax of curl command
curl https://android.googleapis.com/gcm/send -H 'Content-Type: Application/json' -H 'Authorization: key=sdfwer24s:345sdfwer25dfg9dVtpddf345dfg' -d '{"registration_ids":["sdfg34fsg3s:345fw4tsfgsdftg3w5esrert-345"], "data":{"data":"sdf23sdf dsf"}}'

There are problems in AUTH_KEY and registration Ids, because they contains symbols :.

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.