apijenkinsjenkins-pipelinejenkins-pluginssonarqube-api

Http 401 error while invoking api in Jenkins pipeline


I am getting 401 error while hitting sonar api from Jenkins pipeline but from postman I am getting proper response for same url with same credentials, however in Jenkins I am getting below error

Response Code: HTTP/1.1 401 
20:26:38  Response: 

Here is my URL

http://www.sonarqube.com/api/measures/component?componentKey=projectKey&metricKeys=major_violations,info_violations,blocker_violations,minor_violations,critical_violations&includetrends=true&resolved=false

I have tried both the methods(below) but nothing is working for me.

def getJSONResp(url,credentials) {
echo(credentials)
def content
    String auth = credentials.bytes.encodeBase64().toString()
    def json = httpRequest  consoleLogResponseBody: true, 
                 httpMode: 'GET',
                 responseHandle: 'NONE', 
                 url: url,                       
                 customHeaders:[[name:'Authorization', value:"Basic ${auth}"]]
    content = readJSON text: json.content
    return content
}
def getJSONResp(url,credentials) {
echo(credentials)
def content
    def json = httpRequest  consoleLogResponseBody: true, 
                 httpMode: 'GET',
                 responseHandle: 'NONE', 
                 url: url,                       
                 customHeaders:[[name:'Authorization', value:"Basic ${credentials}"]]
    content = readJSON text: json.content
    return content
}

Can someone please help me to resolve this issue.


Solution

  • Append colon(:) to end of auth token, lets say actual auth token is "a26xxxxxxxxxxxxxx" while passing credentials to Basic auth append colon at end of token as "a26xxxxxxxxxxxxxx:" and call the method

    def getJSONResp(url,credentials) {
    echo(credentials)
    def content
        def json = httpRequest  consoleLogResponseBody: true, 
                     httpMode: 'GET',
                     responseHandle: 'NONE', 
                     url: url,                       
                     customHeaders:[[name:'Authorization', value:"Basic ${credentials}"]]
        content = readJSON text: json.content
        return content
    }