Changing special characters from extracted regex in Jmeter

I extracted the API key and JMeter doesn't like special characters with the extracted regex.

Below is an example

I used the regex extractor to extract data from the previous page. The highlighted value: TEST | 1TWO3-TEST

Error message

When using this value later, I get the following message:

java.net.URISyntaxException: Illegal character in query at index 5: (URL+Regex)
    at java.net.URI$Parser.fail(Unknown Source)
    at java.net.URI$Parser.checkChars(Unknown Source)
    at java.net.URI$Parser.parseHierarchical(Unknown Source)
    at java.net.URI$Parser.parse(Unknown Source)
    at java.net.URI.<init>(Unknown Source)
    at java.net.URL.toURI(Unknown Source)
    at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sample(HTTPHC4Impl.java:286)
    at org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:74)
    at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1146)
    at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1135)
    at org.apache.jmeter.threads.JMeterThread.process_sampler(JMeterThread.java:434)
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:261)
    at java.lang.Thread.run(Unknown Source)

      

Hard coded value

When hard-coded, the value he likes works great: TEST% 7C1TWO3-TEST

Any advice on how I can get the regex to look like my hardcoded value through the regex extractor?

+3


source to share


4 answers


Thanks for your answers, I did the following on it.

String var1 = vars.get("var1"); 
String newVar1 = var1.replaceAll("\\|","%7C");
vars.put("var1", newVar1);

      



thank

0


source


You need to url encode.

In the HTTP request , make sure you check "Encode?" in the parameter table:

enter image description here

If the parameter is in the path, you can move it to the table for GET requests.



If you need code, then use JSR 223 Post Processor after your extractor, check your Cache script and add if your regexp extractor generates varName like:

 import java.net.URLEncoder;
 String value = URLEncoder.encode(vars["varName"], "UTF-8");  
 vars.put("ENCODE_VALUE", value);

      

Then you can use the variable via $ {ENCODE_VALUE}

+2


source


Your mistake is not in the regex, but in the way you use the data. After the value is retrieved using a regular expression, make sure you encode the data like this:

String q = "TEST|1TWO3-TEST";
String url = URLEncoder.encode(q, "UTF-8");

      

Now you can use it url

for whatever operation you did. I assumed you have already extracted TEST|1TWO3-TEST

into a variable.

Test

// Output: TEST%7C1TWO3-TEST

      

+1


source


Just wrap the variable coming from the extent of the regex with the urlEncode () function, for example:

${__urlencode(${foo})}

      

Demo:

JMeter urlencode function

Check out How to Use JMeter Functions for more information on using functions in JMeter tests.

There are also several useful features available through the JMeter Plugins Project .

0


source







All Articles