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?
You need to url encode the parameter.
In HTTP Request , ensure you check "Encode ?" in the parameters table:
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}
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
Just wrap your variable coming from the Regular Expression Extractor with urlEncode() function like:
${__urlencode(${foo})}
Demo:
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.
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
Related
I'm trying to write my own Jekyll plugin to construct an api query from a custom tag. I've gotten as far as creating the basic plugin and tag, but I've run into the limits of my programming skills so looking to you for help.
Here's my custom tag for reference:
{% card "Arbor Elf | M13" %}
Here's the progress on my plugin:
module Jekyll
class Scryfall < Liquid::Tag
def initialize(tag_name, text, tokens)
super
#text = text
end
def render(context)
# Store the name of the card, ie "Arbor Elf"
#card_name =
# Store the name of the set, ie "M13"
#card_set =
# Build the query
#query = "https://api.scryfall.com/cards/named?exact=#{#card_name}&set=#{#card_set}"
# Store a specific JSON property
#card_art =
# Finally we render out the result
"<img src='#{#card_art}' title='#{#card_name}' />"
end
end
end
Liquid::Template.register_tag('cards', Jekyll::Scryfall)
For reference, here's an example query using the above details (paste it into your browser to see the response you get back)
https://api.scryfall.com/cards/named?exact=arbor+elf&set=m13
My initial attempts after Googling around was to use regex to split the #text at the |, like so:
#card_name = "#{#text}".split(/| */)
This didn't quite work, instead it output this:
[“A”, “r”, “b”, “o”, “r”, “ “, “E”, “l”, “f”, “ “, “|”, “ “, “M”, “1”, “3”, “ “]
I'm also then not sure how to access and store specific properties within the JSON response. Ideally, I can do something like this:
#card_art = JSONRESPONSE.image_uri.large
I'm well aware I'm asking a lot here, but I'd love to try and get this working and learn from it.
Thanks for reading.
Actually, your split should work – you just need to give it the correct regex (and you can call that on #text directly). You also need to escape the pipe character in the regex, because pipes can have special meaning. You can use rubular.com to experiment with regexes.
parts = #text.split(/\|/)
# => => ["Arbor Elf ", " M13"]
Note that they also contain some extra whitespace, which you can remove with strip.
#card_name = parts.first.strip
#card_set = parts.last.strip
This might also be a good time to answer questions like: what happens if the user inserts multiple pipes? What if they insert none? Will your code give them a helpful error message for this?
You'll also need to escape these values in your URL. What if one of your users adds a card containing a & character? Your URL will break:
https://api.scryfall.com/cards/named?exact=Sword of Dungeons & Dragons&set=und
That looks like a URL with three parameters, exact, set and Dragons. You need to encode the user input to be included in a URL:
require 'cgi'
query = "https://api.scryfall.com/cards/named?exact=#{CGI.escape(#card_name)}&set=#{CGI.escape(#card_set)}"
# => "https://api.scryfall.com/cards/named?exact=Sword+of+Dungeons+%26+Dragons&set=und"
What comes after that is a little less clear, because you haven't written the code yet. Try making the call with the Net::HTTP module and then parsing the response with the JSON module. If you have trouble, come back here and ask a new question.
my problem is that I need to define a regular expression in Jmeter in order to make a DELETE method in my page. The real problem is that I cannot delete a specific token with an ID.
I defined a regular Expression which his name is "uidprofesor" that refers to the token. This token, when I try to delete it manually and catch the request, I get the following code in the Raw Response Data:
{"headers":
{"Location":["/api/profesores/5c745065-3155-4ff7-ac65-7699a36f611c"],
"X-afppApp-alert":["afppApp.profesor.created"],
"X-afppApp-params":["5c745065-3155-4ff7-ac65-7699a36f611c"]},
"body":{"id":"5c745065-3155-4ff7-ac65-7699a36f611c",
"nif":"12345678Q",
"name":"jmeter_122",
"firstSurname":"jmeter_122",
"secondSurname":"",
"address":"",
"postalCode":"",
"telephone":"",
"mobile":"",
"email":"",
"dateInsert":"24/04/2019"
},
"statusCode":"CREATED",
"statusCodeValue":201
}
, where I assume that "5c745065-3155-4ff7-ac65-7699a36f611c" is the ID.
If I wanted to define "uidprofesor" to catch that ID what should I define in the regular erxpression?
I tried defining the regular expression like:
{"headers":"(.+?)"
{"headers":{"Location":"(.+?)"
{"Location":"(.+?)"
But nothing. It shows the Default Value: NOT_FOUND and does not delete the selected token.
I let you here some extra info:
regular expression ${uidprofesor} = {"headers":"(.+?)"
In the HTTP Request the Path is:
https://desa1eap7.asturias.es/afpp-back/${uidprofesor}
If I do the deletion manually, in Google Chrome Developer Tab, It returns:
Requested URL: https://desa1eap7.asturias.es/afpp-back/api/profesores
If you need more info just ask it. Thanks.
Use jMeter JSON Extractor: https://jmeter.apache.org/usermanual/component_reference.html#JSON_Extractor
It uses JsonPath https://goessner.net/articles/JsonPath/
$.headers.Location[0]
Check online here https://jsonpath.com/
I have this reponse in the View tree listener
"__v":0,"language":"en","jobTitle":"Executive","firstName":"wer","lastName":"wre","email":"fde+98989#gmail.com","phone":"+1
213-321-3232","_companyId":"5723erera8b2196fdaer9502b86dc7","accountStatus":"UNVERIFIED","role":"ADMIN","emailActivationSecret":"XD1EererreQyVLFn1IjJg7QQNcDz9RLZ1xrKnl84XI5Nb5","_id":"5723a8b21er96fwereeda9ewree502b86dc8","products":[],"created":"2016-04-29T18:32:18.700Z"}
Now, i need to extract the email, email activation secret and id in to this link:
webserver-wat:3001/activateAccount?email=fde%298989%40gmail.com&secret=XD1EererreQyVLFn1IjJg7QQNcDz9RLZ1xrKnl84XI5Nb5&companyId=5723a8b21er96fwereeda9ewree502b86dc8
And send a post request inorder to activate the link. Is there any way of doing it? Would really appreciate as this is hindering our automation process
I believe it would be easier for you to use JSON Path Extractor (available via JMeter Plugins) as this way you will be able to easily parse multiline data, use conditional selectors and JSON Path expressions look much more friendly and human-readable than regex.
For instance:
To extracting a single value, i.e. email use the following JSON Path Extractor configuration:
Destination Variable Name: anything meaningful, i.e. email
JSON Path Expression: $.email
Refer extracted value as ${email} where required
Repeat above steps (use separate JSON Path Extractor) for other dynamic variables
If you want everything in a single shot:
Destination Variable Name: any suitable variable name, i.e. param
JSON Path Expression: $.[email, emailActivationSecret, _id]
Refer extracted values as:
${param_1} - for "email"
${param_2} - for "emailActivationSecret"
${param_3} - for "_id"
References:
Using the XPath Extractor in JMeter (scroll down to "Parsing JSON")
JSONPath - XPath for JSON
JSONPath Online Evaluator
the regex:
/.*"email":"(.*?)".*emailActivationSecret":"(.*?)".*"_id":"(.*?)".*/
the replacement string:
"webserver-wat:3001/activateAccount?email=$1&secret=$2&companyId=$1"
demo
I have been searching high and low for a solution to this, but to no avail. I am trying to prevent users from entering poorly formed URLs. Currently I have this regular expression in place:
^(http|https)\://.*$
This does a check to make sure the user is using http or https in the URL. However I need to go a step further and validate the structure of the URL.
For example this URL: http://mytest.com/?=test is clearly invalid as the parameter is not specified. All of the regular expressions that I've found on the web return valid when I use this URL.
I've been using this site to test the expressions that I've been finding.
Look I think the best solution for testing the URL as :
var url="http://mytest.com/?=test";
Make 2 steps :
1- test only URL as :
http://mytest.com/
use pattern :
var pattern1= "^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)([A-Za-z]){2,3}(\/)?";
2- split URL string by using pattern1 to get the URL query string and IF URL has Query string then make test on It again by using the following pattern :
var query=url.split(pattern1);
var q_str = query[1];
var pattern2 = "^(\?)?([0-9A-Za-z]+=[0-9A-Za-z]+(\&)?)+$";
Good Luck,
I believe the problem you are having comes from the fact that what is or is not a valid parameter from a query string is not universally defined. And specifically for your problem, the criteria for a valid query is still not well defined from your single example of what should fail.
To be precise, check this out RFC3986#3.4
Maybe you can make up a criteria for what should be an "acceptable" query string and from that you can get an answer. ;)
I want to extract the key value from the JSON response received for a login web service and pass that key value to all other subsequent services.
Note: This key value is dynamic.
The JSON response looks like this:
{
"key":"jwtjWgwnqIVapQeDh47rkFaKV6E",
"username":"Tester",
"password":"xyz",
"servername":"123.45.6.789",
"mailpath":"mail/tmail5.nsf",
"pin":"xxxx",
"defaultAttachments":"OFF",
"sendMailsFor":"W-1",
"serverTime":"10/09/2012 20:02:57"
}
Please help me in writing the reqular expression to extract the same and to pass that key value to all other next web services.
Please let me know what should I write in the fields "Reference Name:", "Regular Expression:", "Template:", "Match No.:" and how to declare the "Reference Name" in the next web services.
Thanks!
If key appearance is unique in response you may simply use Regular Expression Extractor added to the HTTP Request which returns json response, with regex like following:
HTTP Request
Regular Expression Extractor
Reference Name: authKey
Regular Expression: "key":"(.+?)"
Template: $1$
Match No.: 1
and refer extracted value as ${authKey} in all the further requests/samplers across your test.
As well you can look into this for the same situation.
And please don't be lazy in future to look first a bit around: it can appear that answer to your question already exists here.