I'm trying to send HTTP request with binary content using JMeter.
In the documentation I found that I can send file with binary content. I think this is not a good solution for my need, since every request has its own binary content.
Here is an example of a client I wrote in Perl that demonstrates what I tried to accomplish:
$date_time = sprintf "%08X", time();
$BODY_TEMPLATE = "00${date_time}0015";
$body_len = (length (sprintf($BODY_TEMPLATE,0,0))) / 2;
# Here I set $TARGET & $HOST
$MSG_HEADER = "POST \/unil?URL=${TARGET} HTTP\/1.1\r\nHost: ${HOST}\r\ncontent-type:application/binary\r\ncontent-length: ${body_len}\r\n\r\n";
$body = pack ("H*", $BODY_TEMPLATE);
$message_to_send = $MSG_HEADER . $body;
# at this point I sent the entire message over a TCP socket I previously opened.
Any Ideas?
Thanks,
Yuval
It's possible using JMeter. I would recommend using
Beanshell Pre-Processor to construct the body of your request and store it to JMeter Variable (you don't need to calculate content length as JMeter is smart enough to do it for you)
Setting up a HTTP Sampler with "Body Data" generated in Beanshell Pre-Processor (at the end of body generation code you'll need to do something like vars.put("mybody",generatedbodystring);. In your HTTP Sampler you can refer mybody variable as either ${mybody} or ${__V(mybody)}
Adding HTTP Header Manager to set Content-Type to "application/binary"
HTTP Header Manager and BeanShell Pre Processor must be children of your HTTP Request.
You can use Debug Sampler and View Results Tree Listener to inspect request/response details and data being sent.
Hope this helps.
Related
I want to send a data that has been encryption.
So, I used JSR223 Sampler.
---skip---
def encrypted = Crypto.encrypt("AES/CBC/PKCS5Padding", "{"MY_SENTENCE"}", Crypto.generateSecretKey(), Crypto.generateIv())
vars.put("enc_data", encrypted);
Body Data in HTTP Request.
{ "data": "${enc_data}" }
In Results Tree
Request Body data was not url-encoding.
I want to send a data of url encoding, what should I do?
I don't know that.
I wrote Body Data in HTTP Request. So, I can't click the Parameters.
And I added Content encoding (UTF-8) it was not working too.
Either use __urlencode() function in the Body Data tab directly:
{ "data": "${__urlencode(${enc_data})}" }
JMeter Functions can be placed anywhere in the test plan and they're evaluated in the place where they're present so your ${enc_data} variable will be url-encoded in the runtime. See Apache JMeter Functions - An Introduction article for more information on JMeter Functions concept.
or call URLEncoder.encode() function in your Groovy script directly:
vars.put("enc_data", URLEncoder.encode(encrypted,"UTF-8"));
I have a ForEach Controller that loops over an array of values which works. Inside the ForEach block I have two HTTP requests and one extractor.
ForEach Loop {
+ HTTP Request 1 (This uses attribute from ForEach)
+ Extractor
+ HTTP Request 2 (This uses attribute from Extractor)
}
The first HTTP request runs and the extractor as well but the second HTTP request fails to run.
I've checked the logs and nothing is mentioned about this HTTP Request. I also tried moving the Extractor out from under the HTTP Requestor 1 but that also doesn't work.
There is no problem to have multiple Samplers as ForEach Controller children
The possible reasons for not executing the 2nd HTTP Request are in:
Your extractor fails somewhere somehow, double check that the variable is set and has expected value using Debug Sampler and View Results Tree listener combination
Your 2nd HTTP Request configuration is not correct, i.e. invalid characters in "Server Name" field or unsupported protocol or the request is actually being executed but takes too long, etc. I would recommend looking into jmeter.log file as normally it has enough troubleshooting information
I have a request, which gives upload url as response body.
{
"uploadUrl": "https://test.com:9000/sample_uploadurl"
}
I'm able to extract the uploadUrl using JSON extractor.
I want to use above upload url to in next http request. How to set the new request here ?
adding directly doent work, because JMeter prepends http/https before request, in this case we already have https.
It got failed because it has https://[https://test.com:9000/sample_uploadurl]
Leave HTTP Request fields empty except Path, put there the variable and it will be executed
As a special case, if the path starts with "http://" or "https://" then this is used as the full URL.
example
The options are in:
Put the whole ${UPLOAD_URL} to "Path" field of the HTTP Request sampler like:
however this approach might be flaky and cause the malfunction of certain configuration elements like HTTP Cookie Manager. So it's better to go for option 2.
Split the ${UPLOAD_URL} into individual components like protocol, host, port and path.
Add JSR223 PostProcessor after the JSON Extractor
Put the following Groovy code into "Script" area:
URL url = new URL(vars.get('UPLOAD_URL'))
vars.put('protocol', url.getProtocol())
vars.put('host', url.getHost())
vars.put('port', url.getPort() as String)
vars.put('path', url.getPath())
The above code will generate the following JMeter Variables
So you should be able to use the generated variables in the next HTTP Request sampler:
I'm a newbie in using Jmeter and searching for a solution solving the following problem: I have a suite that contains a set of atomic HTTP requests and the goal is to save response bodies into an xml file. Currently, I'm handling that with using "Save Responses to a file listener", but need to be able to save separately error and OK responses in different XML, based on response HTTP code and body string.
to illustrate the case with dummy code
if (HTTP response code == 200 OK && body does not contain "ERROR") then
save response to file (%path%\responsename_OK.xml
else
save response to file (%path%\responsename_ERROR.xml
My bad, Nikolay, I forgot you wanted it in xml format...
Well, the easiest way is to add 2 Simple Data Writers (SDW) to each HTTP Request where 1 SDW logs only errors and the other logs only successes.
The Pro's are
it's easy and once you set it up for 1 HTTP Request, you can simply copy it to every other HTTP Request
Each success or failure with be appended to the corresponding .xml file
You can define as much or as little detail as you want
The Con is you add 2 elements to each Request, thus increasing your test size twice as much as you would with a BeanShell Assertion. (if that's a concern for you.)
If you want a more detailed way, you could try a BeanShell Assertion scripted with XML Parsers and XPath Assertions like outlined here.
Use Response Assertion (in fact you will need 2: one for Response Code and another one for Response Body checking) in order to fail the request basing on these criteria
Use If Controller for setting a condition:
${JMeterThread.last_sample_ok} will be triggered if previous sampler is successful
!${JMeterThread.last_sample_ok} - will fire if previous sampler fails
N.B. you should have any Sampler under the If Controller so it could work.
I am trying to implement a logic where i do not want to send HTTP Requests unless an API returns 0 for a field. If i send the requests without monitoring response from this API, my test is invalid. API returns response in JSON, i can parse and extract the data i need to compare. Below is my test structure.
Thread Group
* While Controller
* CSV Data Set Config
* HTTP Request
* JSON Path Extractor
* Constant Throughput Timer
* While Controller Condition: ${__BeanShell(source("function.bsh”))} != “0”
I want to call this API after every 5 minutes and proceed with sending HTTP Requests when field value is 0. I don't want to check before every HTTP Request just once before sending the first request.
Can someone please help me with the beanshell code (function.bsh) to get API response and parse json response ? Was implemented using python as below
max_behind = 0
response = requests.get("https://api")
consumers = response.json().get("consumers")
for total_behind in consumers.iteritems():
max_behind += total_behind[1].get("total_behind")
As Jmeter developers suggested "Better to avoid scripting languages in Jmeter". So in your case use "If Controller" to resolve your blocker. Below are the details
Use regular expression to extract the "API return field" value, once you hold this value in a variable "valueIs". As shown in belo image
Use "If controller " and in condition enter the following value "${valueIs}==1", then add child requests ( next API call/calls) to "If controller". As shown below image