如何用Jenkins中的httpRequest封装http请求

在Jenkins中经常会与其他服务活着组建有通信,因此掌握编httpRequest时很有必要的,我们在编写自定义的工具时,需要对httpRequest有一个整体上的认识,他和我们通常意义上的使用方法有很多共同点,但同时也有些差别,如果不了解这些,将会走很多弯路,下面将分别讲述

在Jenkins中的编写自定义函数,需要掌握的语言是groovy,因为shared-lib是使用groovy脚本实现的,你可以理解为java的shell版本,也就是命令式编程。下面我们用java语言作为对比来理解它。

共同点:
  • 请求类型一致,都包含POST, PUT, GET, DELETE
  • 都可以直接使用java语法来编程
差别:
  • groovy中的headers声明为customHeaders,对应到java语法上的headers
  • groovy语法无需显式调用execute方法请求
  • 调用接口的方式不同

下面列举出四种常用的例子说明

  • POST请求:
def response = httpRequest contentType: 'APPLICATION_FORM',
      httpMode: "POST",
      customHeaders: [
       [name: "Authorization", value: "Basic xskjasdjkf="]
     ],
     requestBody: "key=key1&value=value1&key2=value",
     url: ''
    
 println response.status
 println response.content
  • PUT请求
def response = httpRequest contentType: 'APPLICATION_JSON',
      httpMode: "PUT",
      customHeaders: [
       [name: "TOKEN", value: "B456skjasdjkf="]
     ],
     url: ''
    
 println response.status
 println response.content
  • GET请求
def response = httpRequest contentType: 'APPLICATION_JSON',
      httpMode: "GET",
      customHeaders: [
       [name: "TOKEN", value: "B456skjasdjkf="]
     ],
     url: ''
    
 println response.status
 println response.content
  • DELETE请求
def response = httpRequest contentType: 'APPLICATION_JSON',
      httpMode: "DELETE",
      customHeaders: [
       [name: "TOKEN", value: "B456skjasdjkf="]
     ],
     url: ''
    
 println response.status
 println response.content
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

banche168

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值