Questions & Answers

Check condition on curl response code and response body

I want to check the condition on curl response code and response body. The response body contain the enum(Success or Failure) I have to check condition on that too.

So basically, I will the send the request using CURL , if there are error responses I will print error, if not I will check the condition on the response body's enum for success or failure, if failure I will print error.

I am facing the problem in extracting response code and body from the response, and parsing them. Other answers only shows how to extract response code separately. I want to work with them at the same time and not sent separate request for both. I am thinking of extracting the code and body in one variable then parsing them. Code examples would help

status_code=$(curl localhost:9090/employee/get/1 -I -w "%{http_code}\n")


2023-01-18 23:52:10
Shos us your code
2023-01-18 23:52:10
@GillesQuenot I haven't written much code yet, this will be hardly 5 to 6 lines code. But I have posted the code for extracting the status code .
Answers(1) :

Like this:


data=$(curl localhost:9090/employee/get/1 -I -w "%{http_code}\n")
status_code=$(awk 'END{print}' <<< "$data")

case "$status_code" in
        echo "ERR $status_code" >&2 
        echo "OK $status_code"
        echo "Not implemented ERR $status_code" >&2