编写 Pipeline
httpRequest命令的 官方文档
发送请求的语句使用 httpRequest
,基本的写法是:
groovy
httpRequest "http://your.site"
httpRequest "http://your.site"
返回一个 response 对象,包含响应的状态码和主体:
groovy
def response = httpRequest "http://your.site"
println('Status: '+response.status)
println('Response: '+response.content)
def response = httpRequest "http://your.site"
println('Status: '+response.status)
println('Response: '+response.content)
默认参数是请求的URL,其他参数加在后方:
groovy
httpRequest url:"http://your.site", httpMode: "GET", ignoreSslErrors: true
httpRequest url:"http://your.site", httpMode: "GET", ignoreSslErrors: true
再有 Post 的例子(application/x-www-form-urlencoded 类型):
def response = httpRequest \
contentType: 'APPLICATION_FORM',
httpMode: "POST",
requestBody: "a=1&b=2",
url: "http://your.site"
def response = httpRequest \
contentType: 'APPLICATION_FORM',
httpMode: "POST",
requestBody: "a=1&b=2",
url: "http://your.site"
注意这里 httpRequest 后面用了 \ 用来表示行间连续(continuation)
另一个Post 的例子(application/json):
def requestBody = ["k1":"v1","k2":"v2"]
def response = httpRequest \
httpMode: "POST",
ignoreSslErrors: true,
contentType: 'APPLICATION_JSON',
requestBody: groovy.json.JsonOutput.toJson(requestBody),
url: "http://your.site"
println response.content
注意这里 httpRequest 后面用了 \ 用来表示行间连续(continuation)
另一个Post 的例子(application/json):
def requestBody = ["k1":"v1","k2":"v2"]
def response = httpRequest \
httpMode: "POST",
ignoreSslErrors: true,
contentType: 'APPLICATION_JSON',
requestBody: groovy.json.JsonOutput.toJson(requestBody),
url: "http://your.site"
println response.content
通过 HttpRequest
拿到响应后,如果响应是 Json 格式的,我们还可以进一步解析 Json 响应,并提取其中的部分内容。解析 Json使用内置函数 readJSON
,基本用法为:
groovy
readJSON text: response.content
readJSON text: response.content
注意这里和上节的 HttpRequest
一样,都需要有命名参数(named argument),即 text:
不可省略。
假设我们收到下面这样的响应:
json
{
"code": 200,
"message": "OK",
"data": ["apple", "banana", "cherry"]
}
{
"code": 200,
"message": "OK",
"data": ["apple", "banana", "cherry"]
}
就可以用下面的语句解析:
groovy
def props = readJSON text: response.content
def code = props['code']
def data = props['data']
def data0 = props['data'][0]
def data1 = props['data'][1]
def props = readJSON text: response.content
def code = props['code']
def data = props['data']
def data0 = props['data'][0]
def data1 = props['data'][1]
在 HTTP 响应中搜索
如果响应中有列表,我们希望从列表中找出第一个满足需要的项,除了普通的遍历方法外,还可以用find方法, 中的响应结果中找到 字母 b 开头的项,可以用下面的方法:
groovy
def props = readJSON text: response.content
def data = props['data']
data.find{
if(it.startsWith("b")){
return true
}
return false
}
def props = readJSON text: response.content
def data = props['data']
data.find{
if(it.startsWith("b")){
return true
}
return false
}
完整的例子
一个完整的 请求httprequest + 解析Json结果的 pipeline 的例子如下:
groovy
pipeline{
agent {label 'slave'}
stages{
stage('Example'){
steps{
script{
def body = [k1]("v1","k2":"v2")
def response = httpRequest \
httpMode: 'POST' ,
ignoreSslErrors: true,
contentType: 'APPLICATION_JSON',
requestBody: groovy.json.JsonOutput.toJson(body),
url: "http://your.site"
def props = readJSON text: response.content
def data = props['data']
data.find{
if(it.startsWith("b")){
return true
}
return false
}
}
}
}
}
}
pipeline{
agent {label 'slave'}
stages{
stage('Example'){
steps{
script{
def body = [k1]("v1","k2":"v2")
def response = httpRequest \
httpMode: 'POST' ,
ignoreSslErrors: true,
contentType: 'APPLICATION_JSON',
requestBody: groovy.json.JsonOutput.toJson(body),
url: "http://your.site"
def props = readJSON text: response.content
def data = props['data']
data.find{
if(it.startsWith("b")){
return true
}
return false
}
}
}
}
}
}
HTTP Request 插件的其他参数
HTTP Request 插件 的 介绍页面 列有详细的参数列表,下面简单介绍部分参数的用法:
consoleLogResponseBody
, 在 Jenkins 控制台打印出响应的body,取值:true/false,等效于:
groovy
println "Response: \n" +response.content
println "Response: \n" +response.content
customHeaders
, 请求的自定义header ,用 Groovy 的数组表示
groovy
customHeaders: [ [name: "k1", value: "v1"], [name: "k2", value: "v2"] ],
customHeaders: [ [name: "k1", value: "v1"], [name: "k2", value: "v2"] ],
outputFile
,将响应的body写入文件,后面的参数是文件的路径,可以是绝对路径或者相对路径。如果写相对路径(或者单独一个文件名),那么起始点是此次构建的workspace目录。
groovy
outputFile: "output.txt"
outputFile: "output.txt"
quiet
,不向控制台打印任何内容。默认情况下,控制台上会出现以下内容:
yaml
HttpMethod: POST
URL: https://XXXX
Content-Type: application/json
Sending request to url: https://XXXX
Response Code: HTTP/1.1 302 Found
HttpMethod: POST
URL: https://XXXX
Content-Type: application/json
Sending request to url: https://XXXX
Response Code: HTTP/1.1 302 Found
当 quiet
设置为 true
后,以上这些内容都不出现在控制台上,这个选项优先于上面的 consoleLogResponseBody
validResponseCodes
,是否检验返回状态码,如果不在此范围内,则请求失败。采取类似下面的格式:
groovy
validResponseCodes: "200:210,300:302,400:403"
validResponseCodes: "200:210,300:302,400:403"
这里冒号表示范围,多个范围之间用逗号隔开