Rest Api Groovy Unauthorized

Maciej Olszewski November 6, 2017

Hello,

I want to create post function based on REST API but i have problem because with being Unauthorized

This is base code in which i wanted to test connections with rest api

import groovyx.net.http.HTTPBuilder
import net.sf.json.JSONArray

def JIRA_URL = "https://myjiraurl"
def JIRA_API_URL = JIRA_URL + "/rest/api/2/"
def jira = new HTTPBuilder(JIRA_API_URL)
jira.auth.basic "login","password"
def test = jira.get(path: 'issue/XXX-5555')

 i get this errors:
groovyx.net.http.HttpResponseException: Unauthorized at groovyx.net.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:651) at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:503) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:222) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:164) at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515) at groovyx.net.http.HTTPBuilder.get(HTTPBuilder.java:285) at groovyx.net.http.HTTPBuilder.get(HTTPBuilder.java:255) at groovyx.net.http.HTTPBuilder$get$0.call(Unknown Source) at Script146.run(Script146.groovy:8)

I don't know what to do with that :( I think that is because of that my jira instance is on HTTPS not HTTP but i am not sure, can somebody help me?

1 answer

1 accepted

2 votes
Answer accepted
adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 8, 2017

This is a quirk of HTTPBuilder. I think your hitting the issue described here.

Following the second example there should help you solve your problem.

Although it looks like your just hitting the issue REST API, you should be able to get all the issue data through the Java API without hitting the REST API.

The following should work for you if you want to do this via the REST API:

import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

def remote = new HTTPBuilder("https://myjiraurl")
def issueJson = remote.request(Method.GET) { req ->
uri.path = "/rest/api/2/issue/XXX-5555"
headers.'Authorization' =
"Basic ${"login:password".bytes.encodeBase64().toString()}"

response.success = { resp, json ->
json ?: [:]
}
}
Maciej Olszewski November 20, 2017

Hello Adam,

Sorry for lat response :( Can you show me example of rest api with script runner? I tried with solutions from your link but it doesn't work (or i dont know how to use it)

cheers,
Maciej

adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 20, 2017

I've updated my answer with what should work for you. You'll need to change username and password along with the issue key.

Hope this helps.

Maciej Olszewski November 20, 2017

Thanks! You are AWESOME!!!! :D

Naman Mandli March 1, 2019

I am able to get access_token but now how do I get access_code out of response and use it for second REST API call by adding it in header? 

 

Please HELP.

Suggest an answer

Log in or Sign up to answer