Skip to content

Instantly share code, notes, and snippets.

@stobias123
Created August 13, 2018 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stobias123/d1759e0d61e58ea94e4994c98566ab4c to your computer and use it in GitHub Desktop.
Save stobias123/d1759e0d61e58ea94e4994c98566ab4c to your computer and use it in GitHub Desktop.
scrubbed sriptrunner
import groovyx.net.http.*
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.json.JsonBuilder
import groovy.json.StreamingJsonBuilder;
RESTClient myClient
myClient = new RESTClient("https://tower_hostname.com/api/v2/authtoken")
myClient.headers = composeRequestHeaders()
private Map<String, String> composeRequestHeaders() {
Map<String, String> headerMap = new HashMap<>()
headerMap.'Content-Type' = 'application/json'
headerMap.'Accept' = 'application/json'
return headerMap
}
def requestBody = [
username : "username",
password : "password",
]
def json = new JsonBuilder( requestBody )
log.error json
HttpResponseDecorator responseToPost =
(HttpResponseDecorator) myClient.post(path: "/", body: json, requestContentType: org.apache.http.entity.ContentType.APPLICATION_JSON)
if (responseToPost.status == 201) {
def actualResourceObject = responseToPost.data
log.error actualResourceObject
}
log.error responseToPost.getStatus()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment