Yes, this is related to Getting curl to output HTTP status code? but unfortunately not the same.
In a script I would like to run:
curl -qSfsw %{http_code} URL
where the -f
option ensures that the exit code is non-zero to signal an error. On success I want to get the (textual) output from the fetched file, whereas otherwise I want to use the error code.
Problem:
- Due to race conditions I must not use more than a single HTTP request
- I cannot use a temporary file for storage of the content
How can I still split the HTTP return code from the actual output?
Pseudo code:
fetch URL
if http-error then
print http-error-code
else
print http-body # <- but without the HTTP status code
endif
Best Answer
There is no need to use a temporary file. The following bash script snippet send a single request and prints the exit code of
curl
and the HTTP status code, or the HTTP status code and response, as appropriate.head -n-1
(print all but the last line) requires GNU, doesn't work on BSD/OS X.