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

相关推荐
xuanzdhc17 分钟前
Linux 基础IO
linux·运维·服务器
真实的菜21 分钟前
Jenkins生态与拓展:构建现代化DevOps工具链的终极指南
servlet·jenkins·devops
愚润求学22 分钟前
【Linux】网络基础
linux·运维·网络
小和尚同志2 小时前
29.4k!使用 1Panel 来管理你的服务器吧
linux·运维
小米里的大麦9 天前
014 Linux 2.6内核进程调度队列(了解)
linux·运维·驱动开发
程序员的世界你不懂9 天前
Appium+python自动化(三十)yaml配置数据隔离
运维·appium·自动化
算法练习生9 天前
Linux文件元信息完全指南:权限、链接与时间属性
linux·运维·服务器
浩浩测试一下9 天前
渗透测试指南(CS&&MSF):Windows 与 Linux 系统中的日志与文件痕迹清理
linux·运维·windows·安全·web安全·网络安全·系统安全
小生云木9 天前
Linux离线编译安装nginx
linux·运维·nginx
19899 天前
【Dify精讲】第19章:开源贡献指南
运维·人工智能·python·架构·flask·开源·devops