1.插件安裝
需要安裝HTTP Request 插件;安裝方式不介紹。
2.請求pipline語法
官網(wǎng)鏈接,上面有詳細(xì)語法:https://plugins.jenkins.io/http_request/
附一個demo,腳本脫敏處理
stage("開啟推送") {
steps {
script {
// 請求sonar獲取信息
echo "請求sonar獲取信息"
def response = httpRequest \
httpMode: "GET",
ignoreSslErrors: true,
contentType: 'APPLICATION_JSON',
// requestBody: groovy.json.JsonOutput.toJson(requestBody1),
url: "http://localhost:9000/"
println('Response: '+response.content)
println()
println("=================================response msg=========================================")
def props = readJSON text: response.content
def bugs = props['measures'][0]['history'][0]['value']
def code_smells = props['measures'][1]['history'][0]['value']
def vulnerabilities = props['measures'][2]['history'][0]['value']
println("bugs: ${bugs}")
println("code_smells: ${code_smells}")
println("vulnerabilities: ${vulnerabilities}")
// robot
def response2 = httpRequest \
httpMode: "POST",quiet: true,
ignoreSslErrors: true,
contentType: 'APPLICATION_JSON_UTF8',
requestBody: '''{
"card": {
"elements": [
{
"tag": "div",
"text": {
"content": "**代碼**,掃描結(jié)果:xxxxxxxx。",
"tag": "lark_md"
}
},
{
"actions": [
{
"tag": "button",
"text": {
"content": "更多詳細(xì)信息 :玫瑰:",
"tag": "lark_md"
},
"type": "default",
"url": "http://localhost:9000/",
"value": {}
}
],
"tag": "action"
}
],
"header": {
"title": {
"content": "靜態(tài)代碼掃描",
"tag": "plain_text"
}
}
},
"msg_type": "interactive"
}''',
url: "https://xxxxxxxx"
}
}
}
3.插件方式實現(xiàn)
直接在構(gòu)建里找到插件選擇就行,不描述
4.亂碼問題解決
編碼字符集問題,解決起來代碼pipline方式比較難解決,就到插件里配置看是否會發(fā)生同樣問題,發(fā)現(xiàn)一樣問題在插件里解決 選擇報文類型 contentType: ‘APPLICATION_JSON_UTF8’ 于是 在pipline里
contentType 類型做對應(yīng)修改即可。文章來源:http://www.zghlxwxcb.cn/news/detail-696462.html
5.值得注意
def props = readJSON text: response.content 這個是解析json文件用的,這個需要安裝一個插件
應(yīng)該是這個 忘記了 Pipeline Utility Steps。百度一下就知道了。文章來源地址http://www.zghlxwxcb.cn/news/detail-696462.html
到了這里,關(guān)于jenkins如何請求http接口及亂碼問題解決的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!