Jmeter truncating the request - jmeter

I am trying to create a script, one of the request sends a json payload with '\\' but JMeter seems to truncate '\' from the request. Please see the details below:
Original Request(from website):
{\"payloadJSON\":\"{\\"formFields\\":[{\\"fieldId\\":\\"replaceItems\\",\\"stringValue\\":\\"
Actual Request(from JMeter):
{\"payloadJSON\":\"{\"formFields\":[{\"fieldId\":\"replaceItems\",\"stringValue\":\"
I have tried adding '\' before '\\' but it doesnt work.
Need advise to make it work. Thanks in advance.

JMeter is a Java application and uses Java Strings. Therefore you have to encode a \ with \\. So for two backslashes you need: \\\\.

Related

Hit to retrieve `state` and `nonce` from the response using regular expression in JMeter

I have the following response,
code\u0026state=cb26ce12-f536-4461-b3ef-339ad846b03e\u0026response_mode=form_post\u0026nonce=75025267-ea92-46b3-8b90-8b78c11d6071\u0026uaid=360422b0f1004bb3a03423ae2f2afb1e\u0026msproxy=1\u0026issuer=mso\u0026tenant=common\u0026ui_locales=en-US\u0026signup=1\u0026lw=1\u0026fl=easi2","urlMsaLogout":"https://login.live.com/logout.srf?iframed_by=https%3a%2f%2flogin.microsoftonline.com","urlOtherIdpForget":"https://login.live.com/forgetme.srf?iframed_by=https%3a%2f%2flogin.microsoftonline.com","showCantAccessAccountLink":true,"urlGitHubFed":"https://login.live.com/oauth20_authorize.srf?client_id=b4f99d51-f6d7-41ce-a058-5b7834afa240\u0026scope=openid\u0026redirect_uri=https%3a%2f%2fsprnt-app05.perceptive.cloud%2fsprint04-webimpact%2fapplication%2fstart.do\u0026response_type=code\u0026state=cb26ce12-f536-4461-b3ef-339ad846b03e\u00
want to retrieve nonce and state using the regex extractor.
I have tried with .*state=(.*).* but not working specially with the escape character \u0026, any clue how to retrieve ?
How about state=(.+?)\\u0026 ?
Meta characters need to be escaped with another backslash
Also if you have hard time coming up with a proper regular expression you can always use Boundary Extractor instead, it's much easier to use and it works faster.

Jmeter beanshell Encounter ";"

I am trying to execute beanshell script in jmeter for a URL parameter value. I have the following:
${__BeanShell(vars.get("query").replaceAll(" ","%20"))}
The jmeter console outputs this:
Caused by: bsh.ParseException: In file: inline evaluation of: ``vars.get("query").replaceAll(" ";'' Encountered ";" at line 1, column 33.
I can't figure out what the problem is as the character there is a , not a ;.
You're doing something ridiculous. Encoding an URL is not only about escaping spaces, looking into URLEncoder documentation you will need to handle:
All non-Latin non-alphanumeric characters
All characters apart from ., -, *, and _
Which might be very tricky.
So you basically have 2 options:
Use JavaScript encodeURIComponent() function via JMeter's __javaScript function like:
${__javaScript(encodeURIComponent("${query}"),)}
Or use aforementioned URLEncoder from __groovy() function like:
${__groovy(URLEncoder.encode(vars.get('query')\, 'UTF-8').replaceAll('\\\+'\,'%20'),)}
Performance-wise in cases of high loads Groovy is preferred option, check out Apache Groovy - Why and How You Should Use It article for more details.
See JMetrer's functions tutorial, you need to escape every comma:
If a function parameter contains a comma, then be sure to escape this with "\", otherwise JMeter will treat it as a parameter delimiter.
In your case
${__BeanShell(vars.get("query").replaceAll(" "\,"%20"))}
Also consider using __groovy function instead of __BeanShell for better performance.
Please use below code in Beanshell PreProcessor or BeanShell PostProcessor in order to replace single space character to '%20':
String myString = vars.get("query");
String new_var = myString.replaceAll(" ", "%20");
vars.put("updated_value", new_var);
You can further use 'updated_value' variable having space replaced by '%20' in next requests.
Please refer to the JMeter Knowledge Base for more information on JMeter elements.

Ajax.oncomplete escaping the singlequote incorrectly

I'm using the Omnifaces ajax.oncomplete function to show a toastr message in my JSF 2 page. The problem I'm facing is that I'm developping a frensh web application and we use a lot of quotes ('). When I add the quote, the browser throws a malformed XML exception :
malformedXML: missing ) after argument list
While I'm using the braqueted slash to make it ignore the quote and treat it as a string :
Ajax.oncomplete("toastr.warning('Vérifier l\'adresse e-mail saisie.')");
Is there a way to pass this exception ?
The \ is also an escape character in Java itself. So, ultimately the \ got stripped off by Java.
You need to double-escape it to represent a literal \, so it arrives as a real \ in JavaScript.
Ajax.oncomplete("toastr.warning('Vérifier l\\'adresse e-mail saisie.')");
Alternatively, if those strings are not hardcoded and thus coming from a dynamic source, and you'd basically thus need to perform automatic escaping, then better use Ajax#data() to automatically let OmniFaces encode the Java variable as a properly formatted JSON object available via OmniFaces.Ajax.data in JavaScript context.
Ajax.data("message", "Vérifier l'adresse e-mail saisie.");
Ajax.oncomplete("toastr.warning(OmniFaces.Ajax.data.message)");
This way you don't need to worry about escaping fuss.

Curl POST request with an array of strings as a paramter

I want to send a curl post request with query params. One of these params is an array of strings. An example for such a string (using Postman chrome extension):
http://www.example.com/my_rest_endpoint?start=123456&duration=11&array_param=["activity level"]
Now, how can I translate this request to a curl command? no matter what I do, I can't seem to send the last parameter, array_param to curl. I've tried escaping the quotes and/or brackets with \, tried using -i flag and other various options, all with no success.
Solution was very close to what benaryorg suggested: Brackets and spaces needs to be placed with %5B, %5D and + according to the url encoding. The quotes were escaped using \". The final result is:
http://www.example.com/my_rest_endpoint?start=123456&duration=11&array_param=%5B\"Activity+Level\"%5D".
Have you tried placing ' around the parameters instead of escaping?
Also if that does not work, you could try to escape them like this example from Wikipedia.
You can find the codes for encoding here.

Jmeter - How Can I Replace a string and resend it?

I'm trying to create a script that will take a URL out of a response and send it out again.
Using the regular expression extractor I've succeeded in taking the wanted URL, but it holds "&" so naturally when sending it out the request fails.
Example:
GET http://[ia-test01.inner-active.mobi:8080/simpleM2M/ClientUpdateStatus?cn=WS2006&v=2_1_0-iOS-2_0_3_7&ci=99999&s=3852719769860497476&cip=113-170-93-111&po=642&re=1&lt=0&cc=VN&acp=&pcp=]/
I'm trying to replace the "&" with a "&".
I've tried: ${__javaScript(${url}.replace("&","&"))}
But it did not work. I've tried the regex function as well- the same.
I'm not sure the IP field in the request supports the us e of functions.
I'm currently trying to use the beanshell post-processor. But I'm pretty sure there is a simpler solution I'm missing.
Not sure what you're trying to get by replacing & with & however will try to respond.
First of all: given multiple & instances you need to use replaceall function, not replace
Second: replace / replaceall functions take a RegEx as parameter, so you'll need to escape your &
If you're trying to substitute URL Path in realtime, you'll need Beanshell Pre Processor, not the Post Processor
Sample Beanshell Pre-Processor code
import java.net.URL;
URL myURL = sampler.getUrl();
String path = myURL.getPath();
String path_replaced = path.replaceAll("\\&", "&");
vars.put("NEW_PATH", path_replaced);
After that put ${NEW_PATH} to "Path:" section of your HTTP Request.
Hope this helps.
Solution with less code:
Install the Custom JMeter Functions plugin
Use the following syntax
${__strReplace(ImAGoodBoy,Good,Bad,replaceVar)}
‘ImAGoodBoy’ is a string in which replacement will take place
‘Good’ is a substring to be replaced
‘Bad’ is the replacement string
‘replaceVar’ is a variable to save result string
Refer this URL for more info!
Thank a lot. However, i see from a recent experience that to replace a character that is actually a RegExp special character, like \ " ( ) etc, you need to put 3 backslashes and not 1, not 2. This is weird.
so you write
var res = str.replaceAll("\\\\u003c", "<");
to replace \u003c with <

Resources