Skip to content
git-j edited this page Apr 25, 2014 · 1 revision

Emulate client with the curl command line tool

During development it is useful to quickly check that the implemented services

  • return the desired data
  • apply the authentication restrictions
  • perform within expected parameters

To check that it is possible to use the curl command line tool which allows to script multiple requests and check the returned data.

Directory Listing

curl --request PROPFIND  --header "Content-Type: text/xml" http://localhost:80/nonexistentpath --data-binary "<?xml version="1.0" encoding="utf-8" ?><D:propfind xmlns:D="DAV:"><propname/></D:propfind>"

should output

<?xml version="1.0" encoding="utf-8"?>
<d:error xmlns:d="DAV:" xmlns:a="http://ajax.org/2005/aml">
    <a:exception>FileNotFound</a:exception>
    <a:message>File at location /nonexistentpath not found</a:message>
<a:jsdav-version>0.3.2</a:jsdav-version>
</d:error>
curl --request PROPFIND  --header "Content-Type: text/xml" http://localhost:80/existentpath --data-binary "<?xml version="1.0" encoding="utf-8" ?><D:propfind xmlns:D="DAV:"><propname/></D:propfind>"

should output

<?xml version="1.0" encoding="utf-8"?>
<d:multistatus xmlns:d="DAV:" xmlns:a="http://ajax.org/2005/aml">
  <d:response>
    <d:href>/existenpath/</d:href>
    <d:propstat>
      <d:prop>
        <d:getlastmodified xmlns:b="urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/" b:dt="dateTime.rfc1123">Thu, 24 Apr 2014 03:27:21 +0200</d:getlastmodified>
        <d:resourcetype>
          <d:collection/>
        </d:resourcetype>
      </d:prop>
      <d:status>HTTP/1.1 200 Ok</d:status>
    </d:propstat>
  </d:response>
  <d:response>
    <d:href>/existentpath/file.txt</d:href>
    <d:propstat>
      <d:prop>
        <d:getlastmodified xmlns:b="urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/" b:dt="dateTime.rfc1123">Thu, 24 Apr 2014 03:27:21 +0200</d:getlastmodified>
        <d:getcontentlength>237806</d:getcontentlength>
        <d:resourcetype/>
        <d:getcontenttype>application/octet-stream</d:getcontenttype>
      </d:prop>
      <d:status>HTTP/1.1 200 Ok</d:status>
    </d:propstat>
  </d:response>
  <d:response>
    <d:href>/existentpath/subdir1</d:href>
    <d:propstat>
      <d:prop>
        <d:getlastmodified xmlns:b="urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/" b:dt="dateTime.rfc1123">Thu, 24 Apr 2014 03:27:21 +0200</d:getlastmodified>
        <d:resourcetype>
          <d:collection/>
        </d:resourcetype>
      </d:prop>
      <d:status>HTTP/1.1 200 Ok</d:status>
    </d:propstat>
  </d:response>
  <d:response>
    <d:href>/existentpath/subdir2/</d:href>
    <d:propstat>
      <d:prop>
        <d:getlastmodified xmlns:b="urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/" b:dt="dateTime.rfc1123">Thu, 24 Apr 2014 03:27:21 +0200</d:getlastmodified>
        <d:resourcetype>
          <d:collection/>
        </d:resourcetype>
      </d:prop>
      <d:status>HTTP/1.1 200 Ok</d:status>
    </d:propstat>
  </d:response>
</d:multistatus>

Download Files

Downloading files is simple and can be done with a simple GET statement

curl http://localhost:80/existent_path/file.binary > file.binary

This will give you the download stats and store the file in the current directory

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  232k  100  232k    0     0  18.2M      0 --:--:-- --:--:-- --:--:-- 18.8M

When downloading plaintext files, they can be displayed on the terminal

curl http://localhost:80/existent_path/file.text

Upload Files

To upload files issue a PUT request:

curl -X PUT http://localhost:80/existent_path/file.text --data "new content"

Delete Files

To delete files issue a DELETE request

curl -X DELETE http://localhost:80/existent_path/file.text

more curl options

Depending on your service additional curl options may be required, eg for basic authentication --basic or to convert \r\n to \n --crlf. Review the manpage of curl for more information.

Clone this wiki locally