Partha YK
Partha YK

Reputation: 1

How to POST a XML file in jmeter body instead of a physical file

I'm using JMeter 3.2. My requirement is to read an XML file from the disk, replace some tags with dynamic values to ensure each thread sends a unique xml file upload (NOT SOAP Request). The following code in JSR223 sampler works perfectly fine when I try to upload the newfile through POST using a http sampler with ${newfilename} file text/xml.

import org.apache.commons.io.FileUtils;

try {

    String content = FileUtils.readFileToString(new File("E:/test.xml"));

        content = content.replaceAll("SUB_ID", "${__UUID}");   
        content = content.replaceAll("ABN_ID", "${empabn}");   
        content = content.replaceAll("EMPNAME", "${empname}");

        vars.put("content", content);
FileUtils.writeStringToFile(new File("E:/testnew${empname}.xml"), content);


} 
catch (Throwable ex) {
log.info("What happened?", ex);
throw ex;
}

Instead of writing again to the disk and uploading again, how can I send the contents of string 'content' as part of request body? I have looked at many posts that talk about the input output streams but they are confusing. When I try to send just ${content} in body, the application throws following error:

HTTP Status 500 - Could not write JSON: Name is null (through reference chain: com.xxx.xxx.datafile.rest.DataFileResponse["validationStatus"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Name is null (through reference chain: 

Appreciate your help.

Upvotes: 0

Views: 2380

Answers (1)

Dmitri T
Dmitri T

Reputation: 168082

Multipart POST requests which are being used for files upload are different from normal POST requests hence there no possibility to simply substitute the file with the generated in-memory string.

You need to replicate the request exactly as it would be send by JMeter or real browser and manually populate each part starting from defining boundary using the HTTP Header Manager and ending up with the creation of Content-Disposition and specify your file contents there.

A little hint: you don't need to generate/substitute values for each call, it is enough to replace them once and JMeter will substitute them on its own given you use __eval() and __FileToString() functions combination.

JMeter Combine Variables

You can check out Testing REST API File Uploads in JMeter for an example of creation a relatively complex file upload request, in your case it will be easier but still tricky.

Upvotes: 0

Related Questions