Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Example: trying to upload a non existing file - curl -d @no-real-file http://google.com | echo $? - returns 0.

How can I make curl fail on input errors?

share|improve this question
up vote 1 down vote accepted

You can use curl -f

-f, --fail  (HTTP) Fail silently (no output at all) on server errors. 
This is mostly done to better enable scripts etc to better deal with failed attempts. 
In normal cases when a HTTP server  fails  to  deliver  a  document,  it returns 
an HTML document stating so (which often also describes why and more). This flag 
will prevent curl from outputting that and return error 22.

This method is not fail-safe and there are occasions where non-successful response 
codes will slip through, especially when authentication is involved (response codes 
401 and 407).

If -f is used, curl will return 22 for the following command

curl -f -d @no-real-file http://google.com
share|improve this answer

This may not be exactly what you asked for, but you could simply do a quick check to see if the file exists beforehand:

[ -f $somefile ] && curl -d $somefile blah blah
share|improve this answer
    
The @no-real-file is just an example. I think there can be other scenarios as well. – AlikElzin-kilaka Mar 12 '14 at 23:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .