我如何在Jenkins Groovy脚本中执行HTTP POST请求? [英] How can I perform HTTP POST requests from within a Jenkins Groovy script?

查看:3647
本文介绍了我如何在Jenkins Groovy脚本中执行HTTP POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要在我们的 Jenkins Pipeline 构建期间创建简单的HTTP POST请求。但是我不能使用简单的curl sh脚本,因为我需要它在Windows和Linux节点上工作,而且我不希望在节点上实施更多的工具安装,如果我可以避免的话。



我们正在使用的Pipeline插件中使用的Groovy库应该完美适用于此任务。有一个可用于Groovy执行简单POST的扩展,称为 http-builder ,但我不能在我的生活中弄清楚如何在Jenkins的Groovy安装中使用它。



如果我尝试使用Grapes Grab在Pipeline脚本中使用它,则会出现错误,请参阅这里

  @Grapes(
@Grab(group ='org.codehaus.groovy.modules .http-builder',module ='http-builder',version ='0.7.1')

Groovy Jenkins使用的捆绑版本可能不支持Grapes Grab。是否有可能简单地下载并添加http-builder及其依赖关系到Jenkins Groovy安装中,并发布到节点上? 解决方案

也许我错过了一些东西,但为什么不使用已经在jenkins类路径中的标准java库?


$ b $

  import java .io.BufferedReader 
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.net.URL
import java.net.URLConnection

def sendPostRequest(urlString,paramString){
def url = new URL(urlString)
def conn = url.openConnection()
conn.setDoOutput(true)
def writer = new OutputStreamWriter(conn.getOutputStream())

writer.write(paramString)
writer.flush()
String line
def reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))
while((line = reader.readLine())!= null){
println line
$ b writer.close()
reader.close()
}

sendPostRequest(http://www.something.com,param1 = abc& param2 = def)


I need to be able to create simple HTTP POST request during our Jenkins Pipeline builds. However I cannot use a simple curl sh script as I need it to work on Windows and Linux nodes, and I don't wish to enforce more tooling installs on nodes if I can avoid it.

The Groovy library in use in the Pipeline plugin we're using should be perfect for this task. There is an extension available for Groovy to perform simple POSTs called http-builder, but I can't for the life of me work out how to make use of it in Jenkins' Groovy installation.

If I try to use Grapes Grab to use it within a Pipeline script I get an error failing to do so, as seen here.

@Grapes(
    @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
)

Maybe Grapes Grab isn't supported in the bundled version of Groovy Jenkins uses. Is it possible to simply download and add http-builder and its dependencies to the Jenkins Groovy installation that goes out to the nodes?

解决方案

Perhaps I'm missing something, but why not just use standard java libraries that are already on the jenkins classpath?

import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.net.URL
import java.net.URLConnection

def sendPostRequest(urlString, paramString) {
    def url = new URL(urlString)
    def conn = url.openConnection()
    conn.setDoOutput(true)
    def writer = new OutputStreamWriter(conn.getOutputStream())

    writer.write(paramString)
    writer.flush()
    String line
    def reader = new BufferedReader(new     InputStreamReader(conn.getInputStream()))
    while ((line = reader.readLine()) != null) {
      println line
    }
    writer.close()
    reader.close()
}

sendPostRequest("http://www.something.com", "param1=abc&param2=def")

这篇关于我如何在Jenkins Groovy脚本中执行HTTP POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆