How to send line break with curl?

前端 未结 9 993
深忆病人
深忆病人 2020-11-27 15:21

I\'ve tried the following to send a line break with curl, but \\n is not interpreted by curl.

curl -X PUT -d \"my message\\n\" http://localhost:         


        
相关标签:
9条回答
  • 2020-11-27 15:49

    Your shell is passing \ followed by n rather than a newline to curl rather than "my message\n". Bash has support for another string syntax that supports escape sequences like \n and \t. To use it, start the string with $' and end the string with ':

    curl -X PUT -d $'my message\n' http://localhost:8000/hello
    

    See ANSI-C Quoting in the Bash Reference Manual

    0 讨论(0)
  • 2020-11-27 15:51

    The solution for someone who doesn't want to use files, and doesn't want to resort to shell escaping magic is:

    curl -X POST --data-binary @- http://url.com <<EOF
    line one
    line two
    EOF
    

    But this is literal newlines in the post data payload, and not in form fields.

    0 讨论(0)
  • 2020-11-27 15:54

    A very easy way, just Shift-Enter in the console for the break. Very readable typing it in too.

    curl -d "line1
    line2" http-echo.com
    
    Server gets this: line1\nline2
    

    Do this to remove the line break:

    curl -d "line1 \
    line2" http-echo.com
    
    Server gets this: line1 line2
    
    0 讨论(0)
提交回复
热议问题