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

相关推荐
北山有鸟8 小时前
用开发板的.config替换ubuntu中内核源码目录的.config
linux·运维·ubuntu
qq_452396239 小时前
第二十篇:《Docker 故障排查常用命令与技巧》
运维·docker·容器
jcbut9 小时前
离线安装dify 1.7
linux·运维·dify
云计算磊哥@9 小时前
运维开发宝典024-Linux云计算运维入门阶段总结
linux·运维·运维开发
周小码9 小时前
10分钟搭建私有Git服务器:Soft Serve实战
运维·服务器·git
The Open Group10 小时前
从项目到产品:数字化转型为何进入“产品时代”?
运维
Urbano10 小时前
休闲束脚裤全生产工艺科普:从裁片到成衣,缝纫工序与自动化设备选型详解
运维·自动化
萤丰信息10 小时前
AI 重构园区运维|全场景无人化智慧运维:破解传统物业高成本痛点
运维·人工智能·重构
AI创界者11 小时前
运维进阶:如何使用 Medusa 进行企业内部服务器密码合规性审计?
运维·服务器
辣椒思密达12 小时前
Python HTTP请求中的重试与超时控制:提升稳定性的实用方法
开发语言·python·http