Here POST request which is using MIME type as Multipart/form-data and parameter as spb I'm passing 1 text file which has complete JSON body.
text file : upload.txt
{
"id":"Rx2160C-019A",
"iAgree":false,
"price":"31.25",
"maxDispenceDays":null,
"RxId":"DRAFT9800E",
.......
}
Every time I need to update the id
and RxId
in the text file based on the last HTTP request.
I was trying do with BeanShell PostProcessor
element, But I am not getting proper code and solution for this.
f = new FileOutputStream("/Users/bhkuma/Documents/Bharath/Jmeter/LoadTesting/goRxDigitize.txt", true);
p = new PrintStream(f);
this.interpreter.setOut(p);
print(name + "," + email);
f.close();
How can i achieve this?
This is the updated code that im trying..
import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedReader;
import org.json.JSONException;
import org.json.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
//ObjectMapper mapper = new ObjectMapper();
//JSONObject root = mapper.readValue(new File("/Users/bhkuma/Documents/Bharath/Jmeter/LoadTesting/upload.txt"), JSONObject.class);
FileReader fr = new FileReader("/Users/bhkuma/Documents/Bharath/Jmeter/LoadTesting/upload.txt");
BufferedReader br = new BufferedReader(fr);
String sCurrentLine;
String output="";
while ((sCurrentLine = br.readLine()) != null) {
output += sCurrentLine;
//System.out.println(sCurrentLine);
}
log.info("bharath");
log.info(output);
JSONObject obj = new JSONObject(output);
JSONObject value = new JSONObject();
value.put("id","Rx2160C-100A");
log.info(value);
obj.put(value);
log.info(output);
System.out.println("Successfully updated json object to file...!!");
and here JSON object wont work...
I've heard Groovy is the New Black so I would recommend switching from Beanshell to JSR223 PostProcessor. The relevant Groovy code to replace id
would be something like:
def upload = new File('/Users/bhkuma/Documents/Bharath/Jmeter/LoadTesting/upload.txt')
def json = new groovy.json.JsonSlurper().parse(upload)
def builder = new groovy.json.JsonBuilder(json)
builder.content.id = 'Rx2160C-100A'
def writer = upload.newWriter()
writer << builder.toPrettyString()
writer.close()
References:
This is the final code that i came up.. its working fine
import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import org.json.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
String path = "/Users/bhkuma/Documents/Bharath/Jmeter/LoadTesting/goRxDigitize1.txt";
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
String sCurrentLine;
String output="";
while ((sCurrentLine = br.readLine()) != null) {
output += sCurrentLine;
//System.out.println(sCurrentLine);
}
log.info("bharath");
log.info(output);
JSONObject obj = new JSONObject(output);
obj.put("id",vars.get("orderId"));
obj.put("RxId", vars.get("RxId"));
//log.info(obj);
//obj.id="test1";
File file = new File(path);
// If file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
// Write in file
bw.write(obj.toString());
// Close connection
bw.close();
//obj.id = "test";
/*JSONObject value = new JSONObject();
log.info(value);
obj.put(value);
log.info(output);*/
//file.write(obj.toString());
System.out.println("Successfully updated json object to file...!!");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.