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

相关推荐
YoungStudyAI5 小时前
QA的下一个自动化阶段,可能不是再写更多脚本,而是设计测试Agent
运维·自动化
分布式存储与RustFS7 小时前
纠删码到底吃掉了多少容量:EC:4、EC:8 的利用率与容错怎么算
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
遇见小修修7 小时前
3步解决打印机0x00000709报错 ,广州深圳程序员可自行排查
运维·打印机·维修·打印机报错·打印机运维·打印机故障自查
Huangjin007_9 小时前
【Linux 系统篇(二十五)】文件(二) 系统文件I/O详解
linux·运维·服务器
微小冷9 小时前
Mermaid画甘特图
运维·markdown·甘特图·mermaid·时间图
LBL122010 小时前
内网容器部署FTP日志监控服务
运维·服务器·容器
志栋智能10 小时前
超自动化巡检:驱动持续改进的运维飞轮
运维·自动化
海宇大数据10 小时前
零信任架构实战:基于海宇学历核验版构建自动化风控审查网关
运维·人工智能·架构·自动化
NailiConveyor10 小时前
输送机系统与WMS仓储管理系统集成的5个关键技术点
大数据·运维·自动化·制造·业界资讯
本人手速666+11 小时前
企微开发API如何设计客户冻结状态?WeComApi 在删除、投诉和异常客户场景中的自动化边界
运维·分布式·自动化·企业微信·企微外部群开发·wecomapi·企业微信二次开发