HTTP NTLM authentication in jenkins pipeline script

Viewed 259

I am trying to post request which requires NTLM authentication. The curl command works fine when i do post call but same method request won't work with jenkins pipeline script.

Curl command:

curl -X POST -k -v -H \"Content-Type: application/json\" -H \"Content-Length: 0\" --ntlm -u domain/username:password http://blrapi/ExeWebAPI/testplans/run/username/89cd1093-6558-4321-b689-cb1

Jenkins Pipeline code

def getClient(){
    def server = ""
    def username = "username"
    def userpassword = "password"
    def domain = "domain"

    def client = new HttpClient()
    client.state.setCredentials(
       AuthScope.ANY,
        new NTCredentials(username, password, "", domain)
    )
    return client
}

def RunPlan( planId ){
    SknetPost("hhttp://blrapi/ExeWebAPI/testplans/run/username/89cd1093-6558-4321-b689-cb1","")
 }

def skynetExecute(httpMethod){
    def httpResponse = ""
    def sknetClient = getClient()

    try {
        int result = sknetClient.executeMethod(httpMethod)
        println "Return code: ${result}"
        httpResponse = httpMethod.getResponseBodyAsString()
    } 
    finally {
        httpMethod.releaseConnection()
    }
    return httpResponse
 }

void SknetPost(url, jsondata) {
    def post = new PostMethod( url )
    post.doAuthentication = true
    post.setRequestHeader("Content-type", "application/json")

    StringRequestEntity requestEntity = new StringRequestEntity( jsonData , "text/html", "UTF-8");
    post.setRequestEntity(requestEntity);
    httpResponse = sknetExecute(post)
    return httpResponse
 }
}

When i execute the program it gives 401- unauthorized access error. Same credentials were used curl command it works fine but in jenkins pipeline it fails.

Please help me to solve this issue.

1 Answers

Web requests with NTLM authentication from Jenkins pipeline could be realized with the HTTP Request Plugin.
Add the Credential (user/password) in the jenkins credential store.
You could then use httpRequest in your pipeline:

script {
    def response = httpRequest httpMode: 'GET',
        url: "http://localhost:80",
        authentication: '3bb9use-your-id-from-store',
        useNtlm: true
    println("Status: "+response.status)
    println("Content: "+response.content)
}

Regards.
work for me with jenkins 2.324, HTTP Request Plugin 1.12

Related