cURL 发送多行文本的问题

2021/09/10

curl -d

$ cat multi_line_file
line 1
line 2
line 3
$ curl localhost:1234 -d @multi_line_file

服务端收到:

$ nc -l 1234
POST / HTTP/1.1
Host: localhost:1234
User-Agent: curl/7.68.0
Accept: */*
Content-Length: 18
Content-Type: application/x-www-form-urlencoded

line 1line 2line 3

-d 选项的默认行为会去除换行符

curl –data-binary

curl --data-binary @multi_line_file localhost:1234

服务端收到:

$ nc -l 1234
POST / HTTP/1.1
Host: localhost:1234
User-Agent: curl/7.68.0
Accept: */*
Content-Length: 21
Content-Type: application/x-www-form-urlencoded

line 1
line 2
line 3

从标准输入中传输多行文本

curl localhost:1234 --data-binary @- << EOF
line 1
line 2
line 3
EOF

RTFM

If you start the data with the letter @, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin. Posting data from a file named ‘foobar’ would thus be done with -d, –data @foobar. When -d, –data is told to read from a file like that, carriage returns and newlines will be stripped out. If you don’t want the @ character to have a special interpretation use –data-raw instead.