How to extract QueryString value using NiFi processor/language expression? - apache-nifi

I am wondering how to extract a querystring value from incoming url using NiFi.
Initially, I started by implementing UpdateAttribute:
For example, I would like from the incoming url http://smth.net/hello?val=23
to have value of 23 extracted.
Further, I expect that I can use that extracted value by referencing it in following way:
(InvokeHttp processor) http://some.net/getValues?id=${q}
Any hints appreciated!
Edited question:
The flowfile inspect after extracting query string looks like:

you could use UpdateAttribute with replaceAll (regular expression):
${url:replaceAll('.*[\\?\\&]val=([^&]*).*','$1')}
maybe regexp could be optimized...

Actually, the easiest possible way to retrieve a query string value is by using NiFi's expression like: ${http.query.param.[keyNameOfQueryString]}.. So, if the request URL is 127.0.0.1/hello?val=23 then the NiFi expression would be ${http.query.param.val}
Other way to do is using regex, as daggett suggested, but in my case I had to do the following:
${http.query.string:replaceAll('val=(\d+).*', '$1')}
Note http.query.string in place of url.

Related

How to add a variable from Json extractor into an array in JMeter?

I'm new here and also a beginner on JMeter and maybe this was already answered in an old post that I didn't find, sorry if this is the case.
I had this Post request I need to send with all these IDs that vary according to the account
Post Request
In order to get all of the IDs, I used the JSon extractor to put then into a variable
JSon extractor, then I got all the FieldIDs that I need.
ID extracted
But now how can I add this variable inside the request? I tried something like {"ids":"${fieldId}","includeBoundary":true} but it didn't work. How can I use this?
Please see: HTTP Request parameter dialog example
If you need to extract the whole response, save it into a JMeter Variable and send it back to another endpoint - the easiest way is using Boundary Extractor providing empty left and right boundaries
If you need more complex transformations - take a look at JSR223 Test Elements and Groovy language
I solved my problem in a so easy way(damn it)!!!!
On the Json extractor I just marked the option "Computer concatenation var (suffix_ALL)" then on the debbuger I got all IDs I needed in only one line and finally on my request I just add on the body data the line {"ids": [${fieldId_ALL}],"includeBoundary":true} and bingo it worked like a charm!!!!

How to handle special charectors in jmeter

In my application one of server value have spacial charector like [[.
Could you please some one let me know how to handle in regular exp
My correlation value is :
"rpc\" : [[\"(.*?)\"
Use \ before for all specal caharecters
Ex like below
"rpc\" : [[\"(.*?)\"
Your server response seems to be JSON therefore it's not best idea to use Regular Expression Extractor for correlation.
Since JMeter 3.0 you have JSON Extractor which allows fetching data from JSON responses using JSONPath query language, in your case it would be something like $..rpc or $..rpc[0]

How I can get a random value string in href by JSON Path Extractor with JMeter?

I have JSON responce
{
"sessionName":"eL7tYgxhYh",
"imageSrc":"/Common/CaptchaImage/**eL7tYgxhYh**?t=636573960197174426"
}
How I can get a random value string "eL7tYgxhYh" in href by JSON Path Extractor with JMeter?
You cannot achieve this using JSON Extractor as it can only deal with JSON Objects, i.e. you can easily get full value of the imageSrc attribute, but not more.
I would recommend switching to Boundary Extractor instead, the relevant configuration would be something like:
Name of created variable: anything meaningful, i.e. href
Left Boundary: CaptchaImage/
Right Boundary: ?
That's it, the "interesting" value should be now extracted and you should be able to refer it as ${href} where required
Demo:
More information: The Boundary Extractor vs. the Regular Expression Extractor in JMeter
JSON format is in key-value pair so you just need to mention the key for which you need to extract the value. As in your case, you can use following JSON path expression for fetching the text mentioned by you:
$.sessionName
You can also use regular expression extractor which is another most important element of JMeter to extract the dynamic variables. Please refer to below blog of RedLine13 for more information on Regular Expressions:
https://www.redline13.com/blog/2016/01/jmeter-extract-and-re-use-as-variable/
Let me know if you have any further question
Above figure shows how to access access_token response of json and store it in access_token variable. In your case need to replace below.
JSONPath Expression use
$..sessionName
Destination variable Name
SessionName
more information for accessing json path
To Get "636573960197174426"
To get required string from result string you can use BeanShell Assertion to split the string.
And Use link to fetch the vaiable properties and jmeter elements
To Fetch jmeter Elements

Passing variable from response to request in JMETER

In need to pass a data from a response to the subsequent request. Something goes wrong and the default variable value appears in the request.
First request returns the JSON in response body which looks like this:
{"issued_at":"2016-01-14T12:41:01.000Z","expires":"2016-01-14T12:46:01.000Z","id":"j6M ... MTA=="}
I extract the value of the id attribute using the Regular Expression Extractor:
Then I pass the token variable to the subsequent request parameter:
But the request is created with the default value of the variable:
There is a JSON Path Extractor designed to deal with JSON content type, I believe it would be easier to use it.
The relevant JSON Path query will be as simple as $..id
See Using the XPath Extractor in JMeter (scroll down to "Parsing JSON") for comprehensive information on plugin installation and usage and JSONPath - XPath for JSON for JSONPath language reference and examples.
In regards to your Regular Expression Extractor configuration:
remove 1 from Match No.
Provide $1$ as Template
If you look at the Regular Expression Extractor documentation, the field Template is required. I suggest you to use value $1$ and try again.
The problem was resolved by setting the "Field to check" radio button to "Body" at the "Regular Expression Extractor" dialog and by setting the Template field value to $1$.
Thanks to alphamikevictor and Dmitri T for help!
You should use ${token_g1} to get the value of the first group of the regex match (the value you're looking for).

JMeter using a variable as input for a response assertion pattern

I have a test plan in JMeter that has a Response Assertion where I'd like to use a variable that comes from a CSV Data Set Config. So my Pattern looks like:
${assert1}
Which corresponds (at least in my thoughts) to what comes from the file used in CSV Data Set Config, but it doesn't work. I have seen multiple suggestions to use a Regular Expression Extractor, but the examples I saw refer to something they're trying to capture from a page. In my case I am using an external csv file.
My question: how can I use a variable as input for a Response Assertion pattern ?
I have successfully used a CSV Data Set Config as a source of string that I use in a url parameter, and then use a Response Assertion to seek out that parameter in the response.
Variable name: P.
URL: /product/${P}.
The response assertion Parterns to Test: ${P}.
I wonder if it is not necessary to use the CSV data variable in the request in order for it to be available in the response assertion?
If my understanding is right,
here your problem:
You want to use the value from your CSV as part of URL and that too via variable.
Solution:
Configure your CSV Data Set Config like this:
Filename: url csv path
variable name : assert1
Delimiter : , (if your CSV comma separated)
leave remaining unchanged
That's it you can use the variable assert1 anywhere: ${assert1}.

Resources