Only display body on error with curl
Sometimes, I only want to display the response from a curl request if it errors – i.e. the status code is 400 or higher. With curl 7.76 or greater this is done using the –fail-with-body parameter: curl -s –fail-with-body \ https://api.example.com/foo/${id} \ -H "Content-Type: application/json" \ –data "@data.json" On success, the return code ($?) is 0 and on failure the body is output with the return code set to 22. Prior to 7.76, you can… continue reading.