1

I am trying to obtain an authentication ticket using a POST request with 3 parameters(user,pass,realm) to access Proxmox API Server to be parsed for further queries.

As I am writing the code in Groovy Script for a parameter in a Jenkins job, I am not getting much help in terms of errors. I have tried the POST request on insomnia and it has no problems.

I am still very new to GroovyScript any pointers in the right direction is much appreciated.

def url = new URL("https://$HOST/api2/json/access/ticket")

def connection =  url.openConnection()
connection.setDoOutput(true)
connection.setRequestMethod("POST")
connection.setRequestProperty("Content-Type", "application/json")
connection.setRequestProperty('Username', '$USER')
connection.setRequestProperty('Password', '$PASS')
connection.setRequestProperty('Realm', '$REALM')
def requestCode = connection.getResponseCode
Tserum
  • 13
  • 5
  • use try-catch `try { ...your code here... } catch(e) { display error here }` to catch and display error. also you could install groovy on your local computer and try to run your script – daggett Mar 18 '19 at 20:37
  • I've got my code now on GroovyConsole, and I am getting a javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed error, i've tried saving the cert using keytools however still no luck(The cert at the https website appears to be invalid) – Tserum Mar 19 '19 at 17:20
  • that's another question )) to work with certificate could be complicated especially if it's a first time for you. try to search `java/groovy trust all` to trust all certificates... – daggett Mar 19 '19 at 17:29
  • Thanks, searching that helped me disable the SSL to further help me troubleshoot. Furthermore, I dont think I am passing the parameters right, I currently tried to do it this way:def url = new URL("https://$HOST/api2/json/access/ticket?username=$USER&password=$PASS&realm=$REALM") Is this correct? – Tserum Mar 19 '19 at 19:40
  • Ask the developers of your API How to pass the auth parameters. – daggett Mar 19 '19 at 19:52
  • Answer found here: https://stackoverflow.com/questions/36115872/how-can-i-perform-http-post-requests-from-within-a-jenkins-groovy-script. – Tserum Mar 19 '19 at 20:01
  • Not need connection.setRequestProperty('Realm', '$REALM'), using Username@realm – Mikhail Zhuikov Feb 11 '21 at 09:25

1 Answers1

0

Not need connection.setRequestProperty('Realm', '$REALM'), using Username@realm try this, write to API:

def url = new URL("https://$HOST/api2/json/access/ticket")
def connection =  url.openConnection()
connection.setDoOutput(true)
connection.setRequestMethod("POST")
connection.setRequestProperty("Content-Type", "application/json")
connection.setRequestProperty('Password', '$PASS')
connection.setRequestProperty('Username', '$USER' + '@' + '$REALM')
def requestCode = connection.getResponseCode
Mikhail Zhuikov
  • 1,213
  • 2
  • 9
  • 19