0

I have a requirement to make a HTTP call using JSR223 sampler using Groovy as language.

Upon making an api call i have received a response and is seen in log viewer.

Now i want to parse and save the returned value in logs to a vaiable for successive usage.

Below is teh sample returned response

2018-05-31 15:38:09,291 INFO o.a.j.p.j.s.JSR223Sampler: {"access_token":"acdaraffafasvtokenhgsvjdk","token_type":"sample","expires_in":80,"scope":"map.c map.r","ext_attr":{"enhancer":"aqa","zdn":"map"},"jti":"jhfeayishdgvs77sdzxcfnkjdx"}

here i want to parse the "access_token" value 'acdaraffafasvtokenhgsvjdk' to a variable for successive usage.

Hari
  • 123
  • 1
  • 14

1 Answers1

0
  1. Right click on the sampler.

  2. Select Post Processor and Select Regular Expression Extractor.

  3. Select the filed you want to check i.e. the body
  4. Under reference name give it the variable name you want (i.e accessToken)
  5. In Regular Expression enter something like "access_token":"(\w+)"
  6. In Match Number enter 1
  7. Add a debug sampler to the test after the http call
  8. Re Run the test and inspect the debug sampler to see you variable

You should then be able to access the variable using ${accessToken} anywhere else in your test..

mkane
  • 880
  • 9
  • 16
  • I am using JSR223 Sampler not HTTP Sampler. – Hari Jun 01 '18 at 11:28
  • Does the solution not work on the JSR223 Sampler Did you try ? – mkane Jun 01 '18 at 11:32
  • If it cannot be used then you could just apply the regex in the code and then use vars.put to add the variable for further use .. – mkane Jun 01 '18 at 11:39
  • If you use a JSR223 Post Processor and the Regex supplied and place Java in there it should work. Have a look at https://www.regular-expressions.info/groovy.html – mkane Jun 20 '18 at 10:13
  • Just looking at the data again it is valid JSON i.e. '{ "access_token": "acdaraffafasvtokenhgsvjdk", "token_type": "sample", "expires_in": 80, "scope": "map.c map.r", "ext_attr": { "enhancer": "aqa", "zdn": "map" }, "jti": "jhfeayishdgvs77sdzxcfnkjdx" }' so you could use the JsonSlurper (refer to [link] https://stackoverflow.com/questions/25775267/groovy-jsonslurper-parsing-json-file) – mkane Jun 20 '18 at 10:21