Allyssa
Allyssa

Reputation: 63

How do I send an authorization token at header when I am using grooy

I am trying to to the same thing like this curl command line

curl -H 'Authorization: token this_is_my_token' this_is_my_url

final DataUrl = new URL("this_is_my_url?access_token=this_is_my_token"
getList new JsonSlurper().parse(DataUrl.newReader())

but it's not working

which I've tried was

def DataUrl =new GetMethod("this_is_my_url");
DataUrl.setRequestHeader("Authorization",token this_is_my_token)
getList new JsonSlurper().parse(DataUrl.newReader())

Upvotes: 0

Views: 459

Answers (1)

ycr
ycr

Reputation: 14604

Your syntax doesn't look correct, for example following line.

DataUrl.setRequestHeader("Authorization",token + "this_is_my_token")

I have used the following method.

def accessToken = "ACCESS_TOKEN" // If you want to Base64 encode use "ACCESS_TOKEN".bytes.encodeBase64().toString()
def get = new URL("https://raw.githubusercontent.com/xxxx/something/hosts").openConnection();
get.setRequestProperty("Authorization", "token " + accessToken)
def content = get.getInputStream().getText()

Upvotes: 1

Related Questions