'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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source