4

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

Yuval
  • 61
  • 1
  • 5

1 Answers1

6

It's possible using JMeter. I would recommend using

  1. 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)
  2. 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)}
  3. 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.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thanks Dmitri. I'm new to Beanhell (and Java). I found that by using: javax.xml.bind.DatatypeConverter.parseHexBinary(hexadecimalString); I can convert my binary data to use it in my code. Once I'll put everything together I hope it will work. – Yuval Nov 27 '13 at 16:22
  • 4
    Dmitri, why you advice to use "generatedbodystring" when author asks for binary data? I have same problem now - cant send binary POST request, even if I do "vars.putObject("mybody", byteArray)" in my BeanShell. Then "${mybody}" in Body Data of HTTP request, Jemeter implicitly do "toString()" of that array before sending, I dont know why and how fix it :( – invis Jul 09 '14 at 13:42
  • I tried the code in https://stackoverflow.com/questions/23101904/how-to-send-byte-array-in-http-request-in-jmeter but the binary data is corrupted when turned into a string. I'd use the HTTP Raw Request Sampler but it doesn't work with https. By using vars.put("requestBody", new String(requestBody, "ISO-8859-1")); I was able to at least get part of the pdf that I'm sending to display correctly. – David Klempfner Feb 22 '21 at 03:52