Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Useful Methods

Expand
titleImport packages
Code Block
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
Expand
titleSend GET request
Code Block
def sendGetRequest(url) {
    def log = Logger.getLogger("LookupManager")
    log.setLevel(Level.DEBUG)

    //Step 1: Set base URL, E.g. http://localhost:2990/jira
    def baseUrl = "http://localhost:2990/jira";

    //Step 2: Set username and password
    def userName = "admin"
    def password = "admin"

    def http = new HTTPBuilder(baseUrl + url)
    http.request(GET) {
        headers.
        'Authorization' = "Basic " + ((userName + ":" + password).bytes.encodeBase64().toString())
        headers.
        'X-Atlassian-Token' = 'no-check';
        requestContentType = URLENC

        response.success = {resp, data ->
            log.debug("SUCCESS: " + resp.status)
            log.debug(data)
        }

        response.failure = { resp,data ->
            log.error("ERROR: " + resp.status)
            log.error(data)
        }
    }
}

...

Expand
titleSend PUT request with data
Code Block
def sendPostRequestsendPutRequest(url, bodyData) {
    def log = Logger.getLogger("LookupManager")
    log.setLevel(Level.DEBUG)
    
    def json = new groovy.json.JsonBuilder()

    //Step 1: Set base URL, E.g. http://localhost:2990/jira
    def baseUrl = "http://localhost:2990/jira";

    //Step 2: Set username and password
    def userName = "admin"
    def password = "admin"

    def http = new HTTPBuilder(baseUrl + url)
    http.request(PUT) {
        headers.
        'Authorization' = "Basic " + ((userName + ":" + password).bytes.encodeBase64().toString())
        headers.
        'X-Atlassian-Token' = 'no-check';
        requestContentType = URLENC
        body = bodyData


        response.success = {resp, data ->
            log.debug("SUCCESS: " + resp.status)
            log.debug(data)
            return data
        }

        response.failure = { resp,data ->
            log.error("ERROR: " + resp.status)
            log.error(data)
            return null
        }
    }
}

...