Login and Session cookie examples in Groovy

  • 0
  • 1
  • Question
  • Updated 1 month ago
  • Answered
I'm trying to perform some rest commands against delphix from Jenkins pipelines.  I have a groovy script trying to get a session, then login token. This is on a windows host.  From the bash examples, it looks like its adding to a cookie with each curl call.

Here's the code:
def delphix_url = "172.31.79.14"def urlString = "http://${delphix_url}/resources/json/delphix";
def login_params = "{\"type\"=\"LoginRequest\"\n"
login_params += "\"username\"=\"delphix_admin\"\n"
login_params += "\"password\"=\"delphix\"\n}"
def api_params = "{\"type\": \"APISession\",\n"
api_params += "\"version\": {\n"
api_params += "\"type\": \"APIVersion\",\n"
api_params += "\"major\": 1,\n"
api_params += "\"minor\": 7,\n"
api_params += "\"micro\": 0}\n"
api_params += "}"

// Do the Session
separator()
println "API Params"
println api_params
URLConnection con = "${urlString}/session".toURL().openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json");
con.outputStream.withWriter { writer ->
  writer << api_params
}
def postRC = con.getResponseCode()
def cresp = con.getHeaderField("Cookies")
//def response = con.inputStream.withReader { Reader reader -> reader.text }
def response = con.getInputStream().getText()
println "URL: " + "${urlString}/session"
println "Result: " + postRC
println "Cookie: " + cresp
println "Response: " + response
def cookie = "user=mary17; domain=airtravelbargains.com; path=/autos"
// Do the Login
separator()
println "Login Params"
println login_params
con = "${urlString}/login".toURL().openConnection();
//con.setRequestProperty("Cookie", cookie)
con.setUseCaches(true)
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json");
con.outputStream.withWriter { writer ->
  writer << login_params
}
postRC = con.getResponseCode()
cresp = con.getHeaderField("Cookies")
//def response = con.inputStream.withReader { Reader reader -> reader.text }
response = con.getInputStream().getText()
println "URL: " + "${urlString}/login"
println "Result: " + postRC
println "Cookie: " + cresp
println "Response: " + response
println response

Here's the output.  You can see, the cookie parameter is empty after getting a 200 on session.
API Params
{"type": "APISession",
"version": {
"type": "APIVersion",
"major": 1,
"minor": 7,
"micro": 0}
}
URL: http://172.31.79.14/resources/json/delphix/session
Result: 200
Cookie: null
Response: {"type":"OKResult","status":"OK","result":{"type":"APISession","version":{"type":"APIVersion","major":1,"minor":7,"micro":0},"locale":null,"client":null},"job":null,"action":null}
#--------------------------------------------------------------------------------#
Login Params
{"type"="LoginRequest"
"username"="delphix_admin"
"password"="delphix"
}
URL: http://172.31.79.14/resources/json/delphix/login
Result: 200
Cookie: null
Response: {"type":"ErrorResult","status":"ERROR","error":{"type":"APIError","details":"No API session has been established.","action":"Create an API session by calling /resources/json/delphix/session.","id":"exception.webservices.session.nosession","commandOutput":null,"diagnoses":null}}
{"type":"ErrorResult","status":"ERROR","error":{"type":"APIError","details":"No API session has been established.","action":"Create an API session by calling /resources/json/delphix/session.","id":"exception.webservices.session.nosession","commandOutput":null,"diagnoses":null}}
Photo of Bradford Byrd

Bradford Byrd

  • 70 Points

Posted 1 month ago

  • 0
  • 1
Photo of Eyal Kaspi

Eyal Kaspi, Employee

  • 1,150 Points 1k badge 2x thumb
Official Response
The header which is set is Set-Cookie, here's an example:
curl -v  -H 'Content-type: application/json' http://<redacted>/resources/json/delphix/session
*   Trying 10.43.79.38...
* TCP_NODELAY set
* Connected to <redacted> port 80 (#0)
> GET /resources/json/delphix/session HTTP/1.1
> Host: <redacted>
> User-Agent: curl/7.54.0
> Accept: */*
> Content-type: application/json

< HTTP/1.1 200 
< Date: Mon, 14 Jan 2019 19:46:40 GMT
< Content-Length: 278
< Connection: keep-alive
< Set-Cookie: JSESSIONID=B36B5FE645AFECCB82CCFDC5EAF10EBD; Path=/resources; HttpOnly
< X-Frame-Options: SAMEORIGIN
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block That being said, I would recommend using an HTTP library (in the language of your choice) as Java's URLConnection is a low level API which is not easy to work with. It might be easier to use Jenkins' HTTP REST plugin or even to call CURL (assuming it is available)
(Edited)