You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
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!
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 must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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]
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.