Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

ScriptRunner REST API GET METHOD

Hi,

 

Could someone show me a simple GET REST API example with username and password?  I have searched and didn't find anything useful for me to use.

 

Thanks in advance!

 

1 answer

1 vote

This will depend on the target system you are trying to connect. They may have different ways of accepting un/pw.

If it's basic authentication then something like this may work:

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

def url ="the url of the system your want to connect to: e.g htts://jira.example.com"
def endpoint = "the endpoint path e.g. /rest/2/issue/XXX-123"
def un = "username"
def pw = "password"
def encoded = "$up:$pw".toString().bytes.encodeBase64().toString()
def returnResponse
def httpBuilder = new HTTPBuilder(url)
try{
httpBuilder.request(Method.GET, ContentType.JSON) {
headers."Authorization" = "Basic $encoded"
uri.path = endpoint
response.success = { response, json ->
if (json) {
returnResponse = json
}
}
response.failure = { resp, data ->
if (data) {
returnResponse = [error: "Failed to make http request to $url/$endpoint", statusLine: resp.statusLine, data: data]
} else {
returnResponse = [error: "Failed to make http request to $url/$endpoint", statusLine: resp.statusLine]
}
}
}
} catch(e){
returnResponse = [error: "Some other error occured: $e"]
}
returnResponse

Thank you Peter-Dave. This code helps me a lot. Also i request you that how to pass the parameter in URL like "/rest/gitplugin/1.0/issues/issueId/commits?showFiles=true"

"/commits?showFiles=true"

You can either manually add it to the uri.path or apply a the query as a simple map object using uir.query. Like this:

uri.query = [showFiles:true]

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Do Not Lose your Customer’s Trust

Missing deadlines is one of the biggest problems every team lead wants to avoid when dealing with managed services. When the customer contracts your company to help with IT services it is expected th...

24 views 0 0
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you