use pipe for curl data

后端 未结 8 465
盖世英雄少女心
盖世英雄少女心 2020-12-23 13:26

I\'m trying to pass the cat output to curl:

$ cat file | curl --data \'{\"title\":\"mytitle\",\"input\":\"-\"}\' http://api

Bu

8条回答
  •  猫巷女王i
    2020-12-23 14:13

    Sounds like you want to wrap the content of input in a JSON body, and then have that sent over with a POST request. I think that the simplest way to do that is to manipulate stdin first and then push that over to curl using -d @-. One way could look like this:

    cat <(echo '{"title":"mytitle","input":"') file <(echo '"}') \
    | curl -d @- http://api
    

    I'm using <(echo) to use cat to merge strings and files, but there is almost certainly a better way.

    Keep in mind that this does not escape the contents of file and that you may run into issues because of that.

提交回复
热议问题