0

I'm getting the parameters from a POST request (x-www-form-urlencoded) formatted as below:

def postreq = exc?.request
def params = postreq?.getPostParams()
println params
Console:
[request:[foo], client:[abcd], id:[qwert]]

How can I use request as key and get its value? Note that there is a extra pair of brackets and the strings are not quoted. I've tried @injecteer suggestions but all I got is caused by NullPointerException: Cannot get property 'request' on null object

I could print all key:value pairs by doing

params.collect{println it.key.toString() + " " + it.value.toString()}
Console:
request [foo]
client [abcd]
id [qwert]

but couldn't get a specific one.

asked Jul 9, 2021 at 4:40
7
  • what is your expected output ? Commented Jul 9, 2021 at 4:52
  • Are the values lists, why the square brackets ? Should have quotes, they're strings: ['request':'[12345]', 'client':'[abcd]', 'id':'[qwert]'] Commented Jul 9, 2021 at 5:42
  • You are stating that something is not working, but you fail to produce what you have tried. So assuming, that there might be more to it, could you please provide a MCVE to show the problem and how the result is not of your liking? Commented Jul 9, 2021 at 11:59
  • @Deadpool foo Commented Jul 9, 2021 at 13:42
  • @ou_ryperd I don't know why, it's how I'm getting the POST parameters... Commented Jul 9, 2021 at 13:43

2 Answers 2

1

You can use

def map = [request:[12345], client:['abcd'], id:['qwert']]
assert map.request == [12345] // dot-notation
assert map[ 'request' ] == [12345] // subscript operator
def name = 'request'
assert map."${name}" == [12345] // Groovy's meta-magical String-interpolated dot-notation
assert map.get( 'request' ) == [12345] // casual java Map's method
assert map.getAt( 'request' ) == [12345] // Groovy's extension of get()
answered Jul 9, 2021 at 8:55
Sign up to request clarification or add additional context in comments.

2 Comments

All I got is caused by NullPointerException: Cannot get property 'request' on null object. I also updated the post with more info. Thanks!
My script runs successfully on groovyconsole.appspot.com . There must be something wrong with the way you are getting the data
0

I only had to convert the variable to string then I could print it:

def params = postreq?.getPostParams()
def reqobj = params?.request.toString()
println reqobj
answered Jul 9, 2021 at 21:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.