Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to make a GET request from Jira to Confluence?

Vineet Kumar
Contributor
January 10, 2019

Hi Everyone, I am able to make a POST request from Jira to Confluence with the help of this code, does any one know how can i modify the below part of the code to make a GET request from Jira to Confluence.

 

authenticatedRequestFactory
.createRequest(Request.MethodType.POST, "rest/api/content")
.addHeader("Content-Type", "application/json")
.setRequestBody(new JsonBuilder(params).toString())
.execute(new ResponseHandler<Response>() {
@Override
void handle(Response response) throws ResponseException {
if(response.statusCode != HttpURLConnection.HTTP_OK) {
throw new Exception(response.getResponseBodyAsString())
}
else {
def webUrl = new JsonSlurper().parseText(response.responseBodyAsString)["_links"]["webui"]
}
}
})

 @JamieA @Jamie Echlin _ScriptRunner - The Adaptavist Group_

1 answer

1 accepted

0 votes
Answer accepted
Tarun Sapra
Community Champion
January 10, 2019

Hello @Vineet Kumar

What is it that  you plan to achieve by doing the Get request?

For the doing the get request you do the change here

.createRequest(Request.MethodType.GET, "rest/api/content")

And also make required changes in the response handler callback code.

Bastian Stehmann
Community Champion
January 10, 2019

Tarun is right, try something like this and replace PAGE_ID_HERE with the ID of the page you want to get.

authenticatedRequestFactory
.createRequest(Request.MethodType.GET, "rest/api/content/PAGE_ID_HERE")
.addHeader("Content-Type", "application/json")
.execute(new ResponseHandler<Response>() {
@Override
void handle(Response response) throws ResponseException {
if(response.statusCode != HttpURLConnection.HTTP_OK) {
throw new Exception(response.getResponseBodyAsString())
}
else {
def webUrl = new JsonSlurper().parseText(response.responseBodyAsString)["_links"]["webui"]
}
}
})
Vineet Kumar
Contributor
January 10, 2019

Thanks Tarun and Bastian, I would like to GET the content of a confluence page and copy it to a variable from the Jason output, but the method is returning null on the script console, May i know which variable on the above script will hold the result in the form of Jason from which i can get the content written on the page.

Bastian Stehmann
Community Champion
January 10, 2019

Change 

def webUrl = new JsonSlurper().parseText(response.responseBodyAsString)["_links"]["webui"]

To new

def body = JsonSlurper().parseText(response.responseBodyAsString)["body"]

 then you can access the body in storage format with

body.storage.value
Sngy August 28, 2020

@Bastian Stehmann I'm using your code to make it possible for users in a Jira Service Desk to create their own Confluence Space by just filling in fields in the Service Desk.

is there any way, so that a user without admin rights can use the Rest Call in order to create a Confluence Space? is there any method in the authenticatedRequestFactory, that might make this possible?

Suggest an answer

Log in or Sign up to answer