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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配