2

I'm making a curl request to a REST API and now I want to save the HTTP answer code, just if an error occurs, to a logfile and the API answer to another file if there is no error occuring.

I'm trying it with:

error=$(curl -v -o "test.json" -H "Authorization: Basic ABCDEF" "https://api.abc.com")

and

error=$(curl --fail -o "test.json" -H "Authorization: Basic ABCDEF" "https://api.abc.com")

If I make an if [0 -eq $? ] after the curl request with --fail I can detect that an error occurred but I am not able to save the HTTP error to a log.

Thanks.

2 Answers 2

2

Since according man curl the option --fail

... 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).

you could use --write-out and http_code.

The numerical response code that was found in the last retrieved HTTP(S) or FTP(s) transfer.

I.e.:

ERROR=$(curl --silent --fail --header "Authorization: Basic ABCDEF" "https://api.abc.com" --output "test.json" --write-out "%{http_code}")
Sign up to request clarification or add additional context in comments.

2 Comments

This works quite perfect, thanks! But, i didn't get this problem with --fail. With your suggestion i would not need the --fail, is that correct?
@reinmanu, right, probably not, since according man curl the option --fail will "enable scripts etc to better deal with failed attempts. In normal cases when an 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."
1

Welcome to stackoverflow.

This should do the trick.


#send all output to file named out
curl -v -o "test.json" -H "Authorization: Basic ABCDEF" "https://api.abc.com" >out 2>&1
# find HTTP/2 code in the output
error=`grep "HTTP/2" out | tail -1 | rev | cut -c1-5 | rev`
# print the error code
echo $error

Example run:

mamuns-mac:jenkins xmrashid$ ./get_error.sh
503 
mamuns-mac:jenkins xmrashid$ 

Good Luck.

2 Comments

I'm working with the output file in some other programs and bashs afterwards and i'm not sure if i can rely on this that there is no other content (e.g. errornumbers which i didn't got out with get) left in the output file... And, with this i get every http code, also the good ones is that right?
Looks like U880D's works better! That's great. Best of luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.