How to send file contents as body entity using cURL

后端 未结 3 1267
悲哀的现实
悲哀的现实 2020-11-29 00:46

I am using cURL command line utility to send HTTP POST to a web service. I want to include a file\'s contents as the body entity of the POST. I have tried using -d &

相关标签:
3条回答
  • 2020-11-29 01:18

    I know the question has been answered, but in my case I was trying to send the content of a text file to the Slack Webhook api and for some reason the above answer did not work. Anywho, this is what finally did the trick for me:

    curl -X POST -H --silent --data-urlencode "payload={\"text\": \"$(cat file.txt | sed "s/\"/'/g")\"}" https://hooks.slack.com/services/XXX
    
    0 讨论(0)
  • 2020-11-29 01:24

    I believe you're looking for the @filename syntax, e.g.:

    strip new lines

    curl --data "@/path/to/filename" http://...
    

    keep new lines

    curl --data-binary "@/path/to/filename" http://...
    

    =====

    curl will strip all newlines from the file. If you want to send the file with newlines intact, use --data-binary in place of --data

    0 讨论(0)
  • 2020-11-29 01:42

    In my case, @ caused some sort of encoding problem, I still prefer my old way:

    curl -d "$(cat /path/to/file)" https://example.com
    
    0 讨论(0)
提交回复
热议问题