Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

HTTPBuilder POST with auth

Gleb Kartashov September 10, 2020

Seems near impossible to make an authenticated request with HTTPBuilder.

Things I tried

http.auth.basic 'login', 'pass'
headers.'Authorization' = 
                "Basic ${"username:password".bytes.encodeBase64().toString()}"
http.setHeaders([Authorization: "Basic encodedAuthString="])

My code:

 def auth = "login:pass"
String authString = auth.bytes.encodeBase64().toString()
def http = new HTTPBuilder('site.com')
http.setHeaders([Authorization: "Basic"+authString])
http.request( POST ) {
uri.path = '/path'
requestContentType = ContentType.JSON
body = someJson
response.success = { resp,reader ->
// ok
}
}

 None of the above worked for me. Request always results with 401 (Unauthorized)

1 answer

0 votes
DH June 29, 2022

Have you tried setting the header inside the function, like so?

def http = new HTTPBuilder('site.com')

http.request( POST ) {
uri.path = '/path'
headers.'Authorization' = 'Basic '+authString
requestContentType = ContentType.JSON
body = someJson
response.success = { resp,reader ->
// ok
}
}

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events