简体   繁体   English

检查 curl 响应代码和响应主体的条件

[英]Check condition on curl response code and response body

I want to check the condition on curl response code and response body.我想检查 curl 响应代码和响应正文的条件。 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.所以基本上,我将使用 CURL 发送请求,如果有错误响应我将打印错误,如果没有我将检查响应主体枚举的条件是成功还是失败,如果失败我将打印错误。

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

Thanks谢谢

Like this:像这样:

#!/bin/bash

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

case "$status_code" in
    4*|5*)
        echo "ERR $status_code" >&2 
    ;;
    2*|3*)
        echo "OK $status_code"
    ;;
    *)
        echo "Not implemented ERR $status_code" >&2
    ;;
esac

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM