Search code examples
javahttpgroovyapache-commons-httpclient

HTTP Get request with Authentication header groovy


I am trying to make a HTTP get request to a URL. I have tried the curl command and it works with my authentication, but in the groovy script I get a NULL response.

import org.apache.commons.httpclient.HttpClient
import org.apache.commons.httpclient.methods.GetMethod

def http = new HttpClient();
def get = new GetMethod("https://tect-hello/cit");
def requestHeader=get.setRequestHeader("X-Vault-Token","s.mytoken");
println requestHeader             //NULL
println get.getName();            //GET
println get.getResponseBody()     //NULL

CURL command

curl -H "X-Vault-Token: s.mytoken" -X GET https://tect-hello/cit

I tried the post functionality and it worked.

I am unable to find a way to pass the vault token. I tried both addRequestHeader and setRequestHeader


Solution

  • So I found how to do the get Method.

    def get = new GetMethod("URL")
    get.setRequestHeader("X-Vault-Token","s.mytoken")
    println http.executeMethod(get) // This step is important
    println get.getResponseBodyAsString(1000)
    

    Hope this helps someone!