Michal
Michal

Reputation: 469

CURL how to call unix error code on http error code

I am calling curl to website, checking what http status code and data if returns.

There are 2 cases:

  1. SUCCESS website HTTP status code: 200 (OK) data: none

needed result: CURL exiting with 0 unix status code

  1. FAILURE website HTTP status code: 4xx (error) - could be any data: text data

needed result: CURL exiting with error status code

For now there is used curl with -f modifier (silent).

curl https://mysite.com/something/deploy_status -f

It works great, because it checks the HTTP status code, and return unix error code (22) when there is any problem. However, I need to get also to get the text data, that is sent with error code.

How can I do that? Assume that I can make only one curl call, writing a script is not possible there.

Upvotes: 0

Views: 1064

Answers (1)

Sabuj Hassan
Sabuj Hassan

Reputation: 39355

This is not possible. Its strictly mentioned in curl's manual page -f Fail silently (no output at all) on HTTP errors (H)

So make another curl call to save the html separately.

curl -o output.html YOUR_URL_GOES_HERE

Or use this one that gets you the http status code along with the html page.

curl -s -w %{http_code} http://www.example.com/test -o output.html

Upvotes: 2

Related Questions