dongqiongjiong4740 2017-06-20 19:58 采纳率: 0%
浏览 58
已采纳

获取并评估Jenkins中的卷曲响应

I am trying to get the response of a Http post with curl in Jenkins, I have the following script:

curl -X POST -k -H "Accept: application/json" -H "Content-Type: application/json" --data-binary "@/var/lib/jenkins/workspace/Folder/sessions.json" http://mypage/Data/file.php

As you can see I am sending to file.php a json file, and then I am calling some functions and am returning a specific result.

With that script, I am getting the result I want, but I want to evaluate that result, let's say for example the result was "OK", then I want to assign the result to a variable, and then say if $result=="OK" then do this else do that. How can I do that, I have tried something like this:

if $response == "true" then exit 1 fi

But it does not seem to work out, does anyone know how it can be done?

They marked it as similar to this question PHP cURL, extract an XML response , but I don't see how, because I am not talking about php code, bash code, and I want to store the curl result in a variable....

Thanks in advance!!!

  • 写回答

1条回答 默认 最新

  • duancai1904 2017-06-20 21:08
    关注

    You can check the status code if you only need to check if the request was successful :

    status=$(curl --write-out '%{http_code}' \
        -s -o /dev/null \
        -H "Accept: application/json" \
        -H "Content-Type: application/json" \
        --data-binary "@/var/lib/jenkins/workspace/Folder/sessions.json" \
        "http://mypage/Data/file.php")
    
    if [ "$status" == "200" ]; then
        echo "request was successful"
    else
        echo "error status : $status"
    fi
    

    with :

    • --write-out '%{http_code}' : output the status code
    • -o /dev/null : doesn't to output the body
    • -s : doesn't display connection log

    As you have specified Accept: application/json, you expect a response in JSON format, so you could use jq JSON parser to parse it :

    If the response is :

    { "status": true }
    

    then you can do the following :

    status=$(curl -s -H "Accept: application/json" \
        -H "Content-Type: application/json" \
        --data-binary "@/var/lib/jenkins/workspace/Folder/sessions.json" \
        "http://mypage/Data/file.php" | jq -r '.status')
    
    if [ "$status" == "true" ]; then
        echo "request was successful"
    else
        echo "error status : $status"
    fi
    

    If the response is not in JSON format and the response is OK :

    status=$(curl -s -H "Content-Type: application/json" \
        --data-binary "@/var/lib/jenkins/workspace/Folder/sessions.json" \
        "http://mypage/Data/file.php")
    
    if [ "$status" == "OK" ]; then
        echo "request was successful"
    else
        echo "error status : $status"
    fi
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化