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


    1.插件安装

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

    2.请求pipline语法

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

    附一个demo,脚本脱敏处理

            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"
                    }
                }
            }
       
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    3.插件方式实现

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

    4.乱码问题解决

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

    5.值得注意

    def props = readJSON text: response.content 这个是解析json文件用的,这个需要安装一个插件
    应该是这个 忘记了 Pipeline Utility Steps。百度一下就知道了。

  • 相关阅读:
    java计算机毕业设计教材订购系统源码+mysql数据库+系统+lw文档+部署
    FCoE测试重启调试记录
    COPU名誉主席陆首群在第十七届开源中国开源世界高峰论坛上的致辞
    皮皮仔!在 vscode 里操作数据库~
    Redis 第五天
    如何选择对应的学习方向呢
    DQN论文阅读
    [springboot源码分析]-Conditional
    APA技术架构与说明
    【云原生】kubernetes中pod的生命周期、探测钩子的实战应用案例解析
  • 原文地址:https://blog.csdn.net/weixin_42439274/article/details/132688445