curlconverter curl转代码

Transpile curl commands into C#, ColdFusion, Clojure, Dart, Elixir, Go, HTTPie, Java, JavaScript, Kotlin, MATLAB, OCaml, PHP, PowerShell, Python, R, Ruby, Rust, Swift, Wget, Ansible, HAR, HTTP or JSON.

curl from Google Chrome

  1. Open the Network tab in the DevTools
  2. Right click (or Ctrl-click) a request
  3. Click “Copy” → “Copy as cURL”
  4. Paste it in the curl command box above

This also works in Safari and Firefox.

Warning: the copied command may contain cookies or other sensitive data. Be careful if you’re sharing the command with other people, sending someone your cookie for a website is like sending them your password.

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 254 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
    • 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 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
  • cfml
  • clojure
  • csharp
  • dart
  • elixir
  • go
  • har
  • http
  • httpie
  • javajava-httpurlconnectionjava-jsoupjava-okhttp
  • javascriptjavascript-jqueryjavascript-xhr
  • json
  • kotlin
  • matlab
  • nodenode-httpnode-axiosnode-gotnode-kynode-requestnode-superagent
  • ocaml
  • phpphp-guzzlephp-requests
  • powershellpowershell-webrequest
  • python (the default)
  • 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.

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.

☆版权☆

* 网站名称:obaby@mars
* 网址:https://obaby.org.cn/
* 个性:https://oba.by/
* 本文标题: 《curlconverter curl转代码》
* 本文链接:https://obaby.org.cn/2023/07/12692
* 短链接:https://oba.by/?p=12692
* 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


You may also like

2 comments

  1. Level 4
    Safari 16 Safari 16 iPhone iOS 16.5.1 iPhone iOS 16.5.1 cn中国–四川–成都 移动

    大佬又发了一些我看不懂的了

    1. 公主 Queen 
      Google Chrome 114 Google Chrome 114 Mac OS X 10.15 Mac OS X 10.15 cn中国–山东–青岛 移动

      这个写自动化代码的时候就很方便,比如要模拟浏览器发送数据神马的。各种参数就不用自己格式化处理了。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注