Rishi
Rishi

Reputation: 115

Change Special Characters From Extracted Regex In Jmeter

I have extracted an API key and JMeter does not like the special characters with the extracted regex.

Below is an example

I used regular expression extractor to extract the data from the previous page. The value extracted is: 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 coding the value it like it works fine: TEST%7C1TWO3-TEST

Any advice regards how I can get the regex to look like my hard coded value via regular expression extractor?

Upvotes: 4

Views: 4429

Answers (4)

Rishi
Rishi

Reputation: 115

Thanks for your replies, I did the following at it worked.

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

Thanks

Upvotes: 0

Dmitri T
Dmitri T

Reputation: 168122

Just wrap your variable coming from the Regular Expression Extractor with urlEncode() function like:

${__urlencode(${foo})}

Demo:

JMeter urlencode function

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

There are also few useful ones available via JMeter Plugins project.

Upvotes: 1

UBIK LOAD PACK
UBIK LOAD PACK

Reputation: 34536

You need to url encode the parameter.

In HTTP Request , ensure you check "Encode ?" in the parameters table:

enter image description here

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

If you end up needing to code, then use JSR 223 Post Processor after your extractor, check Cache script and add if your regexp extractor creates varName for example:

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

You can then use the variable through ${ENCODE_VALUE}

Upvotes: 3

degant
degant

Reputation: 4981

Your error isn't in the regex but afterwords in how you are using the data. After the value is extracted using regex, make sure you encode the data like this:

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

Now you can use url for whatever operation you were doing. I've assumed that you have already extracted TEST|1TWO3-TEST into a variable.

Test

// Output: TEST%7C1TWO3-TEST

Upvotes: 1

Related Questions