1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

curlで文字数の多いファイルの内容をPOST

Posted at

概要

結論だけ言うとcurlコマンドを使って長い文字列をPOSTするときは@-を使って、

$ cat hoge | curl -X POST "http://xxxx.xxx" -d @-

のように書くとうまくいくという話で、いろいろなWebサイトにかかれているけど自分で検証してみたかっただけです。

背景

ファイル内容をcurlでPOSTしようとすると、

$ curl -X POST "http://xxxx.xxx" -d "$(cat hoge)"

$ cat hoge | xargs -IXXX -0 \
curl -X POST "http://xxxx.xxx" -d "XXX"

で実現しようと思うがコマンド長に制限があるため、

/usr/bin/curl: Argument list too long

というようにエラーが出てしまう。

環境

  • WSL(Windows10)上のUbuntu18.04
  • 引数の最大値は以下のとおり
$ getconf ARG_MAX

2097152

実験

準備

長めのファイルを作成。

$ python3 -c "print(('x'*500+'\n')*500)" > file.txt
$ wc file.txt 
   501    500 250501 file.txt

送れているか確認するためのTCPサーバ作成(受信データをファイル出力)。

$ nc -l 8000 > serverout

エラーが出ることを確認

上記の方法で実行してみる。

$ curl -X POST "http://localhost:8000" -d "$(cat file.txt)"
-bash: /usr/bin/curl: Argument list too long
$ cat file.txt | xargs -IXXX -0 curl -X POST "http://localhost:8000" -d "XXX"
xargs: argument line too long

が、駄目。

うまくいくことを確認

ようやく、@-を試す。

$ cat file.txt | curl -X POST "http://localhost:8000" --data-binary @-

サーバ側のデータが受信できることを確認(内容省略)。

$ head serverout

POST / HTTP/1.1
Host: localhost:8000
User-Agent: curl/7.58.0
Accept: */*
Content-Length: 250000
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...

課題

  • --dataで渡すか--data-binaryで渡すか、--data-urlencodeで渡すか
  • ARG_MAXよりも随分小さいサイズでエラーが起きている気がする。

リンク

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?