使用webhook传参的方式

接上篇webhook部署
官网参考地址
码云地址
github地址
参考配置

实现webhook以post方式传送数据

1.大部分情况是传递json格式的数据

参考curl命令的详细使用curl命令使用
curl -H "Content-Type: application/json" -X POST -d '{"info":{"name":"wade.qu", "passwd":"123456","data":"this is test content"}}' http://10.41.1.20:9001/hooks/test-webhook

  1. 配置hooks.json文件接受相关参数配置
    hook.json
    [
    {
    "id": "test-webhook",
    "execute-command": "/tmp/redeploy.sh",
    "command-working-directory": "/tmp",
    "pass-arguments-to-command":
    [
    {
    "source": "payload",
    "name": "info"
    }
    ]
    }
    ]
    2.启动项目 webhook -hooks hook.json -verbose --port 9001
    3.配置脚本接受参数或者打印参数
    redeploy.sh脚本内容
#!/bin/bash
filename=`date +%Y%m%H%M%S`
touch $filename
echo "this is test file ,please test"
echo $1 > $filename

4.发送请求链接进行测试

curl -H "Content-Type: application/json" -X POST -d '{"info":{"name":"wade.qu", "passwd":"123456","data":"this is test content"}}'  http://10.41.1.20:9001/hooks/test-webhook

结果内容如下

root@devops-test:/tmp# cat 202210141848 
map[passwd:123456 data:this is test content name:wade.qu]

5.定义匹配的规则信息以及相关参数
通过token匹配请求是否准入
curl -H "Content-Type: application/jsons" -X POST -d '{"info":{"name":"wade.qu", "passwd":"123456","data":"this is test content"}}' 'http://10.41.1.20:9001/hooks/test-webhook?token=5656'

匹配的hook.json文件内容如下

[
  {
     "id": "test-webhook",
     "execute-command": "/tmp/redeploy.sh",
     "response-message": "ok",

     "command-working-directory": "/tmp",
     "response-headers":
     [
        {
            "name": "Access-Control-Allow-Origin",
            "value": "application/json"
        }
     ],
     "pass-arguments-to-command":
     [
             {
             "source": "header",
             "name": "Content-Type"
             },
             {
             "source": "payload",
             "name": "info.name"
             },
             {
             "source": "payload",
             "name": "info.passwd"
             }
     ],
     "trigger-rule":
     {
        "and":
        [
                {
                "match":
                        {
                        "type": "value",
                        "value": "5656",
                        "parameter":
                        {
                                "source": "url",
                                "name": "token"
                        }
                        }
                }
        ]
     
     }

  }
]

此时如果不带对应的token 将会出现不满足请求的条件的提示
其中 "response-message": "ok" 这个参数是指定返回值的内容(如果请求成功的话)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容