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。百度一下就知道了。

相关推荐
小李独爱秋35 分钟前
模拟面试:什么是容器技术,Docker是什么?
运维·docker·容器·面试·职场和发展
yangyanping201081 小时前
系统监控Prometheus之Docker安装部署Prometheus
运维·docker·容器·prometheus
海兰1 小时前
Elasticsearch 9.3.0 日志数据源配置
大数据·elasticsearch·jenkins
盟接之桥1 小时前
制造业EDI数字化:连接全球供应链的桥梁
linux·运维·服务器·网络·人工智能·制造
大梦想家~2 小时前
在职牛马,因为考过阿里云ACP,浅说下一次过的强度
运维·云计算·网络工程师·阿里云acp·云计算工程师·阿里云acp考试·阿里云acp备考
REDcker2 小时前
HTTP 协议发展详解:从 HTTP/1 到 HTTP/3
网络·网络协议·http
暴力求解2 小时前
Linux-进程(三)进程的孤儿状态和僵尸状态
linux·运维·服务器
乾元2 小时前
数据投毒:如何通过训练数据污染埋下“后门”
运维·人工智能·网络协议·安全·网络安全·系统架构·自动化
小鸡食米2 小时前
Keepalived高可用
运维·服务器·网络
dreams_dream3 小时前
docker清除所有网络
运维·docker·容器