bingoohuang / blog

write blogs with issues
MIT License
177 stars 23 forks source link

变态 curl 的用法 #198

Open bingoohuang opened 3 years ago

bingoohuang commented 3 years ago

来自 logpaste

  1. Upload text: echo "some text I want to upload" | curl -F '_=<-' http://logpaste.com
  2. Upload a file's contents: curl -F "_=@/path/to/file.txt" http://logpaste.com
  3. Use the -d option with a @- argument to accept input from a pipe. : echo "time=uptime" | curl -d @- http://URL

https://man7.org/linux/man-pages/man1/curl.1.html

-F, --form (HTTP SMTP IMAP) For HTTP protocol family, this lets curl emulate a filled-in form in which a user has pressed the submit button. This causes curl to POST data using the Content-Type multipart/form-data according to RFC 2388.

  For SMTP and IMAP protocols, this is the mean to compose a
  multipart mail message to transmit.

  This enables uploading of binary files etc. To force the
  'content' part to be a file, prefix the file name with an
  @ sign. To just get the content part from a file, prefix
  the file name with the symbol <. The difference between @
  and < is then that @ makes a file get attached in the post
  as a file upload, while the < makes a text field and just
  get the contents for that text field from a file.

  Tell curl to read content from stdin instead of a file by
  using - as filename. This goes for both @ and <
  constructs.
bingoohuang commented 2 years ago

看到这么一个 curl 命令,echo Hello world. | curl -F 'f:1=<-' ix.io ,又感觉神奇了,-F 参数是啥意思呢?

还好,我有 httplive 可以作为 Mock http server,回显一下即可。

(其实,echo Hello world. | curl -F 'f:=<-' ix.io 也可以)

$ echo Hello world. | curl -F 'f:1=<-' http://127.0.0.1:5003/echo
{
    "Ua-Bot": false,
    "Ua-Browser": "curl",
    "Ua-BrowserVersion": "7.77.0",
    "Ua-Engine": "",
    "Ua-EngineVersion": "",
    "Ua-Localization": "",
    "Ua-Mobile": false,
    "Ua-Mozilla": "",
    "Ua-OS": "",
    "Ua-OSInfo": {
        "FullName": "",
        "Name": "",
        "Version": ""
    },
    "Ua-Platform": "",
    "headers": {
        "Accept": "*/*",
        "Content-Length": "151",
        "Content-Type": "multipart/form-data; boundary=------------------------affc036d80a4dd60",
        "Proxy-Connection": "Keep-Alive",
        "User-Agent": "curl/7.77.0"
    },
    "host": "127.0.0.1:5003",
    "method": "POST",
    "payload": "--------------------------affc036d80a4dd60\r\nContent-Disposition: form-data; name=\"f:1\"\r\n\r\nHello world.\n\r\n--------------------------affc036d80a4dd60--\r\n",
    "proto": "HTTP/1.1",
    "remoteAddr": "127.0.0.1:65207",
    "requestUri": "/echo",
    "timeGo": "2022-03-10 10:54:16.5562",
    "timeTo": "2022-03-10 10:54:16.5562",
    "url": "/echo"
}

可以看到,作用是提交一个名字为 f:1 的 form data,如果把 < 换成 @, 则多了一个文件 filename。

$echo Hello world. | curl -F 'f:1=@-' http://127.0.0.1:5003/echo
{
    "Ua-Bot": false,
    "Ua-Browser": "curl",
    "Ua-BrowserVersion": "7.77.0",
    "Ua-Engine": "",
    "Ua-EngineVersion": "",
    "Ua-Localization": "",
    "Ua-Mobile": false,
    "Ua-Mozilla": "",
    "Ua-OS": "",
    "Ua-OSInfo": {
        "FullName": "",
        "Name": "",
        "Version": ""
    },
    "Ua-Platform": "",
    "headers": {
        "Accept": "*/*",
        "Content-Length": "165",
        "Content-Type": "multipart/form-data; boundary=------------------------4a15b83a43c9e81e",
        "Proxy-Connection": "Keep-Alive",
        "User-Agent": "curl/7.77.0"
    },
    "host": "127.0.0.1:5003",
    "method": "POST",
    "payload": "--------------------------4a15b83a43c9e81e\r\nContent-Disposition: form-data; name=\"f:1\"; filename=\"-\"\r\n\r\nHello world.\n\r\n--------------------------4a15b83a43c9e81e--\r\n",
    "proto": "HTTP/1.1",
    "remoteAddr": "127.0.0.1:65215",
    "requestUri": "/echo",
    "timeGo": "2022-03-10 10:55:22.1744",
    "timeTo": "2022-03-10 10:55:22.1745",
    "url": "/echo"
}