How can I rewrite this CURL multipart/form-data request without using -F?

后端 未结 7 1492
无人共我
无人共我 2020-12-07 10:29

How can I rewrite the following CURL command, so that it doesn\'t use the -F option, but still generates the exact same HTTP request? i.e. so that it passes th

7条回答
  •  情深已故
    2020-12-07 11:24

    Solved:

    curl \
      -X POST \
      -H "Content-Type: multipart/form-data; boundary=----------------------------4ebf00fbcf09" \
      --data-binary @test.txt \
      http://localhost:3000/test
    

    Where test.txt contains the following text, and most importantly has CRLF (\r\n) line endings:

    ------------------------------4ebf00fbcf09
    Content-Disposition: form-data; name="example"
    
    test
    ------------------------------4ebf00fbcf09--
    

    Notes: it is important to use --data-binary instead of plain old -d as the former preserves the line endings (which are very important). Also, note that the boundary in the body starts with an extra --.

    I'm going to repeat it because it's so important, but that request-body file must have CRLF line endings. A multi-platform text editor with good line-ending support is jEdit (how to set the line endings in jEdit).

    If you're interested in how I worked this out (debugging with a Ruby on Rails app) and not just the final solution, I wrote up my debugging steps on my blog.

提交回复
热议问题