4

While using curl to POST or PUT a file to a url, there are two different ways to load the file content:

  1. -T ${file_path}
  2. --data-binary @${file_path} or -d $@{file_path} (for ascii data files)

What is the difference between these two options?

    1 Answer 1

    2

    To understand differences between these options, you can either read the man page or make some experiments.

    The man page

    -d, --data

    Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded. [...]

    -T, --upload-file

    This transfers the specified local file to the remote URL. [...] If this is used on an HTTP(S) server, the PUT command will be used. [...]

    Make some experiments

    Run netcat in listening mode (macOS version):

    nc -l localhost 8989 

    Create a test file:

    echo test>test 

    Run the curl commands and check the results on the listening side:

    % curl -d "name=test" localhost:8989 POST / HTTP/1.1 Host: localhost:8989 User-Agent: curl/7.84.0 Accept: */* Content-Length: 9 Content-Type: application/x-www-form-urlencoded name=test % curl -T test localhost:8989 PUT /test HTTP/1.1 Host: localhost:8989 User-Agent: curl/7.84.0 Accept: */* Content-Length: 5 Expect: 100-continue test 

    To answer your question, the -d option make a POST request by sending the data with the x-www-form-urlencoded format, and -T send a PUT request without formatting the data.

      You must log in to answer this question.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.