LoginSignup
10
9

More than 3 years have passed since last update.

curl コマンドを Node.js に変換するなら curlconverter が便利かも

Posted at

curlconverter とは

curlconverter は curl コマンドをプログラミングコードに変換してくれるツールです。
下記のURLでブラウザ上で試すことができます。
https://curl.trillworks.com/
スクリーンショット 2020-11-28 23.49.05.png

Python, Node.js, R, PHP, Go, Dart, Elixir, Rust などの言語 JSON への変換などにも対応しているようです。

開発しながらAPI ドキュメントを読んで curl コマンドを自分の開発言語で読み替えている方も多いと思います。
こちらのツールを使わせていただいて自動変換すれば作業が捗りそうです。

GitHub のリポジトリはこちらでした。
NickCarneiro/curlconverter
https://github.com/NickCarneiro/curlconverter

たとえば PayPal Developper のドキュメントの Orders API の curl コマンドをコピペで入力し、変換先に node.js (fetch) を選択してみると、
https://developer.paypal.com/docs/api/orders/v2

curl -v -X POST https://api.sandbox.paypal.com/v2/checkout/orders \
-H "Content-Type: application/json" \
-H "Authorization: Bearer Access-Token" \
-d '{
  "intent": "CAPTURE",
  "purchase_units": [
    {
      "amount": {
        "currency_code": "USD",
        "value": "100.00"
      }
    }
  ]
}'

以下のように fetchを使った node.js 上で動作する JavaScript に変換されました。

var fetch = require('node-fetch');

fetch('https://api.sandbox.paypal.com/v2/checkout/orders', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer Access-Token'
    },
    body: JSON.stringify({ "intent": "CAPTURE", "purchase_units": [ { "amount": { "currency_code": "USD", "value": "100.00" } } ] })
});
10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9