Re: How can I remove leading and trailing double-quotes from a variable in JSR233 Postprocessor?

Dmitri T <[email protected]> Wed, 25 Sep 2024 09:50:07 +0200
Newsgroups gmane.comp.jakarta.jmeter.user
Message-ID <SA3PR05MB1037234FF76B21B429C9CE7E6C0692@SA3PR05MB10372.namprd05.prod.outlook.com>
ohaya wrote:
> Hi,
> I am working on a test plan where I send a POST request to authenticate, and I get a token in the response.
> Then, I need to send the token in GET request to get authorized.
> The problem I am having is that the the response I am getting to the POST has the token surrounded by double-quotes (a double-quote in the front, and a double-quote, or possibly a double-quote followed by a blank/space) and I think that the double-quotes are causing the error.
> I am extracting the token in a JSR233 Postprocessor with the following script:
> vars.put("response", prev.getResponseDataAsString());Can I remove the leading double-quote and the trailing double-quote (or possibly trailing double-quote+space in the same Postprocessor, and if so how can I do that?
> Thanks,Jim
>

If you want to do this in Groovy - amend your code like:

vars.put('response', 
prev.getResponseDataAsString().replaceAll(/^['"\s]+|['"\s]+$/, ''))

or just

vars.put('response', prev.getResponseDataAsString().takeBetween('"'))

Check out Groovy SDK documentation on String class 
<https://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/String.html> 
and The Groovy Templates Cheat Sheet for JMeter 
<https://www.blazemeter.com/blog/groovy-templates> article for more 
useful code examples