-2

I have created a Jenkins freestyle project executing Groovy script, inside which I need to call a REST api to validate username and password. How to pass these parameters and use the entered credentials inside the Groovy script.

This is the code I am using to get the REST api called :

def post = new URL("http://xxxx/xxxxx/xxxxx/xxxx?userName=$Username&password=$Password").openConnection()
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Content-Type", "application/json")

def message = '{"message":"this is a message"}'
post.getOutputStream().write(message.getBytes("UTF-8"))

def postRC = post.getResponseCode()

println(postRC)

But the username and password in the above URL is the one I want from the user building the job. How do I get that?

halfer
  • 19,824
  • 17
  • 99
  • 186
SaswatPatro
  • 13
  • 1
  • 9
  • We are sorry, you are under time pressure, but aren't we all? So please help us save you time, by saving us time. Please add the code you have tried and how it failed so we can improve on it. – cfrick Dec 10 '19 at 10:48
  • I tried giving two string parameters and added Script parameters as : ("Name:Username","Value:$Username","Name:Password","Value:$Password"). I got the value on my console output but i am not able to use those values inside my groovy script – SaswatPatro Dec 10 '19 at 10:54
  • Please [edit](https://stackoverflow.com/posts/59265381/edit) the question with all the relevant informations – cfrick Dec 10 '19 at 10:56
  • I have edited the question – SaswatPatro Dec 10 '19 at 10:59
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Dec 10 '19 at 11:23

1 Answers1

0

Finally after a lost of research i got an answer to my query. Actually,if your job type is of a freestyle job,accessing build parameters through "Execute Groovy Script" only is not possible. For accessing build parameters, you need to use "Execute System Groovy Script" build steps and write your groovy scripts. Reference : Access to build environment variables from a groovy script in a Jenkins build step (Windows)

Thanks!

SaswatPatro
  • 13
  • 1
  • 9