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

Tor*_*ous 7 groovy jenkins-plugins jenkins-pipeline

我试图用"Http Request Plugin"做一个简单的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}"
}
Run Code Online (Sandbox Code Playgroud)

结果是

java.lang.IllegalStateException: Authentication 'user:pass' doesn't exist anymore
Run Code Online (Sandbox Code Playgroud)

Ian*_*n A 13

HTTP Request Plugin v1.8.18 现在支持Credentials插件中的凭据(HTTP Request Plugin v1.8.18 现在依赖于Credentials插件的v2.1.3).

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

def response = httpRequest authentication: 'credentialsID', url: "http://www.example.com"
Run Code Online (Sandbox Code Playgroud)

credentialsIDJenkins中凭据的ID 在哪里:

在此输入图像描述

"配置系统">"HTTP请求"下的基本凭据现在指出不推荐使用基本/摘要式身份验证,而是使用Jenkins凭据:

在此输入图像描述

  • 如果您仍然需要传递基本身份验证的“明文”凭据(例如,您将它们存储在 Vault 中,而不是 Jenkins 凭据中),您可以像在 https://issues.jenkins-ci.org/browse 中那样制作授权标头/JENKINS-39744 另请注意,`customHeaders` 参数现在接受 `maskValue` 来隐藏 username:password 的 base64,请参阅 https://jenkins.io/doc/pipeline/steps/http_request/ 了解更多信息。 (4认同)
  • 我做了与你在答案中提到的一样的事情,但我收到了错误 java.lang.IllegalStateException: Authentication 'CredentialsID' does not exist (2认同)