jenkins如何请求http接口及乱码问题解决

文章目录

1.插件安装

需要安装HTTP Request 插件;安装方式不介绍。

2.请求pipline语法

官网链接,上面有详细语法:https://plugins.jenkins.io/http_request/

附一个demo,脚本脱敏处理

groovy 复制代码
        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": "**代码**,扫描结果:xxxxxxxx。",
                                                        "tag": "lark_md"
                                                    }
                                                },
                                                {
                                                    "actions": [
                                                        {
                                                            "tag": "button",
                                                            "text": {
                                                                "content": "更多详细信息 :玫瑰:",
                                                                "tag": "lark_md"
                                                            },
                                                            "type": "default",
                                                            "url": "http://localhost:9000/",
                                                            "value": {}
                                                        }
                                                    ],
                                                    "tag": "action"
                                                }
                                            ],
                                            "header": {
                                                "title": {
                                                    "content": "静态代码扫描",
                                                    "tag": "plain_text"
                                                }
                                            }
                                        },
                                        "msg_type": "interactive"
                                    }''',
                        url: "https://xxxxxxxx"
                }
            }
        }
   

3.插件方式实现

直接在构建里找到插件选择就行,不描述

4.乱码问题解决

编码字符集问题,解决起来代码pipline方式比较难解决,就到插件里配置看是否会发生同样问题,发现一样问题在插件里解决 选择报文类型 contentType: 'APPLICATION_JSON_UTF8' 于是 在pipline里

contentType 类型做对应修改即可。

5.值得注意

def props = readJSON text: response.content 这个是解析json文件用的,这个需要安装一个插件

应该是这个 忘记了 Pipeline Utility Steps。百度一下就知道了。

相关推荐
Arvin6272 分钟前
docker /var/lib/docker/overlay2磁盘满的处理方法
运维·docker·容器
Bruce_Liuxiaowei5 分钟前
网站敏感文件_目录大全(分类记忆+风险标注)
运维·网络·网络协议·http·网络安全·https
csdn_aspnet15 分钟前
MobaXterm 运维实战、技巧与自动化
运维·自动化·mobaxterm
2201_7611990416 分钟前
jenkins找不到Build when a change is pushed to GitHub
运维·github·jenkins
独角鲸网络安全实验室29 分钟前
高危预警!React核心组件曝CVSS 9.8漏洞,数百万开发者面临远程代码执行风险
运维·前端·react.js·网络安全·企业安全·漏洞·cve-2025-11953
博语小屋31 分钟前
Linux 地址转换函数详解
linux·运维·服务器·c++
饱饱要坚持可持续发展观36 分钟前
Linux 防火墙开放/限制端口
linux·运维·网络
初学者_xuan1 小时前
安装与配置Jenkins(OpenEuler)
运维·servlet·jenkins
SunPengMSE1 小时前
基于运行unitree rl lab 的 Ubuntu 20.04 配置isaac sim + isaac lab
linux·运维·ubuntu
广东大榕树信息科技有限公司1 小时前
机房动环管理如何通过智能可视化实现高效运维?
运维·网络·物联网·国产动环监控系统·动环监控系统