groovy - 使用Jenkins http-request-plugin进行基本身份验证

标签 groovy jenkins-plugins jenkins-pipeline

我正在尝试使用“Http请求插件”进行简单的POST请求。我的问题是要获得凭据才能正常工作。我已经设置了全局凭证user:pass

但是在我的代码中尝试

withCredentials([usernameColonPassword(credentialsId: 'akamai', variable: 'akamai')]) {

    def response = httpRequest url: requestUrl, contentType: requestContentType, httpMode: requestHttpMode, requestBody: requestContent, authentication: akamai
    echo "Status: ${response.status}\nContent: ${response.content}"
}

结果是
java.lang.IllegalStateException: Authentication 'user:pass' doesn't exist anymore

最佳答案

HTTP请求插件v1.8.18 now supports credentials in the Credentials Plugin(HTTP请求插件v1.8.18 now depends on v2.1.3 of the Credentials Plugin)。

要使用Jenkins凭证执行HTTP请求,可以使用以下代码:

def response = httpRequest authentication: 'credentialsID', url: "http://www.example.com"

其中credentialsID是Jenkins中凭据的ID:

enter image description here

现在,“配置系统”>“HTTP请求”下的基本凭据表明已弃用基本/摘要身份验证,而改为使用Jenkins凭据:

enter image description here

关于groovy - 使用Jenkins http-request-plugin进行基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41571090/

相关文章:

java - Groovy Shell 沙箱最佳实践

jenkins - 如何使用 Groovy 高效列出 Jenkins 上当前正在运行的**所有**作业

jenkins - 1 个 jenkins 作业根据参数触发多个 jenkins 作业

java - Jenkins groovy 正则表达式匹配字符串 : Error: java. io.NotSerializableException : java. util.regex.Matcher

regex - 运行 groovy 时 Windows CMD 的奇怪行为

java - 如何从 SAX-Parser 获取所有验证错误?

maven - 尝试从源代码构建 Jenkins 插件时出现 java.nio.file.NoSuchFileException 和 java.io.EOFException

junit - 有 JUnit Attachments Jenkins 插件工作的示例吗?

jenkins - 如何生成 Jenkins 管道图?

jenkins - Jenkinsfile 中的 Anaconda