34

Just wondering how I can send a curl command with the -d option specifying a file with its path and not a file in the current directory.

This is what I'm getting when I try to test my app with the json file in the local dir. Both the app and myself are happy:

curl -XPOST -H 'Content-Type:application/json' -d @all_fields.json http://testcomp.lab.net:8080/stats -v -s
* About to connect() to testcomp.lab.net port 8080
*   Trying 10.93.2.197... connected
* Connected to testcomp.lab.net (10.93.2.197) port 8080
> POST /stats HTTP/1.1
> User-Agent: curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
> Host: testcomp.lab.net:8080
> Accept: */*
> Content-Type:application/json
> Content-Length: 2882
> Expect: 100-continue
> 
< HTTP/1.1 100 Continue
HTTP/1.1 200 OK
< Content-Length: 0
* Connection #0 to host testcomp.lab.net left intact
* Closing connection #0

This is what I'm getting when I specify a json file that's in another directory

curl -XPOST -H 'Content-Type:application/json' -d @json/all_fields.json http://testcomp.lab.net:8080/stats -v -s
"Invalid json for Java type interface java.util.List"
Warning: Couldn't read data from file "json/all_fields.json", this makes an 
Warning: empty POST.

<snip snip>
<snip snip>

< HTTP/1.1 400 Bad Request
< Content-Type: application/json
< Transfer-Encoding: chunked
* Connection #0 to host testcomp.lab.net left intact
* Closing connection #0

I didn't see anything in the man page for curl for specifying directories for files passed in as data. Am I unfortunately limited to files in the local directory or is there a special way to specify files in different directories? Thanks in advance for your help.

Classified
  • 5,759
  • 18
  • 68
  • 99

1 Answers1

59

The -d @ command option accepts any resolvable file path, as long as the path actually exists. So you could use:

  • a path relative to the current directory
  • a fully qualified path
  • a path with soft-links in it
  • and so on

To wit, just the same as hundreds of other *Nix style commands. One quick note, the -d option will attempt to url encode your data, which from what you describe isn't actually what you want. You should use the --data-binary option instead. Something like this:

curl -XPOST
     -H 'Content-Type:application/json'
     -H 'Accept: application/json'
     --data-binary @/full/path/to/test.json
     http://localhost:8080/easy/eservices/echo -v -s
Perception
  • 79,279
  • 19
  • 185
  • 195
  • thanks for replying and answering. putting the relative or absolute path doesn't work and the path does exist. what version curl did you use when you ran your cmd? mine is curl 7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5. i'm almost at wit's end trying to figure out what's going on b/c what you wrote about above is what i did but it's not working. – Classified May 20 '13 at 22:53
  • curl 7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5 – Perception May 21 '13 at 03:37
  • could this be caused by the difference in curl version? the path to the json file is valid. however, for me, when I use the path in the curl cmd, it doesn't work. if i run the curl cmd and the file is in the local dir, then it works. – Classified May 21 '13 at 21:12
  • Are you certain you are using a full path? Or that if you are using a relative path thats it truly exists relative to your working directory? I doubt that the version of curl is the problem (though it might be a possibility). – Perception May 22 '13 at 02:33
  • 1
    Wow, this is crazy. If I run the cmd with the path (absolute or relative) on my MacBook Pro, I keep getting the 400 Bad Request. If I run the cmd with the path on a Centos VM, it works fine. I didn't think there would be a difference between running the cmd in a Mac's terminal vs "real" Linux box. @Perception Thanks for helping me and replying to my question. – Classified May 23 '13 at 18:25