How can i perform a get in Java the way i do in cUrl ?

Diederik de Boer February 6, 2019

I have created an API token in jira and with that token i can do a GET request like this:

curl https://<useremail>:zfO8D8QTrn6M4567cVChF46F@<mycompany>.atlassian.net/rest/agile/1.0/board/13/sprint?state=active

 

When in Java i send this request:

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("https://<useremail>:zfO8D8QTrn6M4567cVChF46F@<mycompany>.atlassian.net/rest/agile/1.0/board/13/sprint?state=active");
        CloseableHttpResponse response1 = httpclient.execute(httpGet);

I get a 401

 

The httpClient is from apache:

'org.apache.httpcomponents:httpclient:4.5.7'

How can i authenticate with HttpClient ??

Allready tried UsernamePasswordCredentials.

 

1 answer

0 votes
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 8, 2019

I'm surprised that your curl with just the URL is able to return anything useful.  Even when using curl, I would expect you to have to pass specific headers in your request in order to get back useful data.   Please see our the Jira Software REST API for that endpoint GET /rest/agile/1.0/board/{boardId}/sprint.

In those documents, there are specific examples of how to make such requests using curl, php, python, and even Java.

The format for Java has this details:

// This code sample uses the 'Unirest' library:
// http://unirest.io/java.html
HttpResponse<JsonNode> response = Unirest.get("https://your-domain.atlassian.net/rest/agile/1.0/board/{boardId}/sprint")
.basicAuth("email@example.com", "<api_token>")
.header("Accept", "application/json")
.asJson();

System.out.println(response.getBody());

You aren't required to use the unirest library like our example, but I expect this to work.

I also found another resource that might be helpful.  It appears to explain some more detailed steps for using the apache httpclient to make rest calls: RESTful Java client with Apache HttpClient

Suggest an answer

Log in or Sign up to answer