1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

curl コマンドを fetch / axios / Python / Go / Ruby など 10 言語に変換するツール

1
Posted at

作ったもの

Curl Converterhttps://sen.ltd/portfolio/curl-converter/

スクリーンショット

  • 10 言語/クライアントに変換: fetch / axios / XHR / HTTPie / Python requests / Python http.client / Node http / PHP curl / Go net/http / Ruby net/http
  • シェル対応トークナイザ
  • 主要な curl フラグ解析 (-X -H -d -u -F --cookie など)
  • バックスラッシュ行継続
  • JSON body 自動検出
  • Basic 認証の自動展開

vanilla JS、ゼロ依存、ビルド不要node --test で 124 ケース。

パーサが本題

コード生成は各言語 30 行のテンプレート。難しいのは curl コマンドを正しく解析すること。ブラウザの「Copy as cURL」が出す実際のコマンドを食わせたい。

シェルトークナイザ

スペース分割では不可能:

  • "..." '...' の引用符内のスペース
  • バックスラッシュ + 改行(行継続)
  • 引用符内のエスケープ
if (c === '\\' && command[i + 1] === '\n') i += 2; // 行継続

シングルクォート内はエスケープ処理しない(POSIX 準拠)。

curl の暗黙動作

-d を指定すると自動的に POST になる。-u user:passAuthorization: Basic ... ヘッダに自動展開される。これらの副作用を忠実に再現することで、実行結果が元の curl と一致する。

各言語のテンプレート

パース済みモデルから各言語のイディオマティックなコードを生成。Python requests なら requests.post(url, headers=..., json=...)、Go なら http.NewRequest ... Ruby なら Net::HTTP::Post.new。同じデータ、違うテンプレート。

シリーズ

100+ 公開ポートフォリオ シリーズの #61 です。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?