Question

I have a post request where I have to send a gzip file as byte array. I have tried the following:

  1. Created a BeanShell preprocessor to convert the file to byte array with the following function(got this function from here) :
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream; 

FileInputStream in = new FileInputStream("C:\Users\New\Desktop\Load_Testing");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int i; (i = in.read(buffer)) != -1; ) {
    bos.write(buffer, 0, i);
}
in.close();
byte[] postData = bos.toByteArray();
bos.close();
vars.put("postData", new String(postData));
  1. Added the parameter postData in HTTP Request Sampler under Parameter's tab as ${postData} . Also tried adding the parameter ${postData} in HTTP Request Sampler under BodyData tab.

But the file is not sent.

  1. I also tried sending the file by adding it under the tab "Send Files With the Request" section in HTTP request sampler with appropriate encoding.

But for all the cases I am getting the error :

:{"Result":"Error uploading data stream
The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.
   at System.IO.Compression.GZipDecoder.ReadHeader(InputBuffer input)
   at System.IO.Compression.Inflater.Decode()
   at System.IO.Compression.Inflater.Inflate(Byte[] bytes, Int32 offset, Int32 length)
   at System.IO.Compression.DeflateStream.Read(Byte[] array, Int32 offset, Int32 count)
   at System.IO.Compression.GZipStream.Read(Byte[] array, Int32 offset, Int32 count)
   at System.IO.Stream.ReadByte()
   at ServiceData.CompressionHelper.UnGZip(Byte[] input) 
Was it helpful?

Solution

First of all, I don't like your file path C:\Users\New\Desktop\Load_Testing. In Java and Beanshell you need to escape back slashes like C:\\Users\\New\\Desktop\\Load_Testing

Second: make sure that you're sending a correct gzip file. If Your C:\Users\New\Desktop\Load_Testing file is not in gzip format, you'll need to convert compress it in your Beanshell Pre-Processor as follows:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.GZIPOutputStream;

byte[] buffer = new byte[1024];
GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream("C:/Users/New/Desktop/Load_Testing.gz"));
FileInputStream in = new FileInputStream("C:/Users/New/Desktop/Load_Testing");

int len;
while ((len = in.read(buffer)) > 0) {
    gzos.write(buffer, 0, len);
}

in.close();

gzos.finish();
gzos.close();

And finally, make sure that you're sending correct Accept-Encoding header. To do so add a HTTP Header Manager and include gzip value in Accept-Encoding stanza.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top