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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号