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.
2 Answers 2
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
Adami
All I got is
caused by NullPointerException: Cannot get property 'request' on null object
. I also updated the post with more info. Thanks!injecteer
My script runs successfully on groovyconsole.appspot.com . There must be something wrong with the way you are getting the data
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
Comments
default
['request':'[12345]', 'client':'[abcd]', 'id':'[qwert]']
foo