2

I am trying to test REST APIs using JMeter. My lead told me to do load testing on each API using JMeter. Currently I am testing a Get API request, and I am getting the below JSON response body.

"https://api.rewards.com:/lists/v1/listcontainer/1?ts=20190221004021&auth=EngineeringSolutions:ydvMMlY2uxiKG0yuwh1IbVgR2mfqTQaQncTEaMr+Ef0="

Now I have to pass this JSON body to another HTTP request and test the API.

My questions:

  1. How can I trim double quotation characters from the JSON response body?
  2. How can I get the values of ts and auth using split method like (ts=20190221004021 and auth=EngineeringSolutions:ydvMMlY2uxiKG0yuwh1IbVgR2mfqTQaQncTEaMr+Ef0=)

I know I can use a Regular expression Extractor or BeanShell PreProcessor to do all the actions, but I don't know how to do it. Can anyone guide me how I can trim and split the JSON response?

1 Answers1

0
  1. You don't trim off doublequotes, they are part of JSON syntax.
  2. There are many methods to split the query string in URL, here are 2 of them.

var url = "https://api.rewards.com:/lists/v1/listcontainer/1?ts=20190221004021&auth=EngineeringSolutions:ydvMMlY2uxiKG0yuwh1IbVgR2mfqTQaQncTEaMr+Ef0=";

//method 1: pure js
var queries = url.match(/(.+)\?(.+)/)[2].split("&");
var params  = {};

for (let i=0; i<queries.length; i++){
  var item = queries[i].match(/([0-9A-Za-z]+)=(.+)/);
  params[item[1]] = item[2]
}

console.log(JSON.stringify(params,null,2));

//method 2: new in ES6
var queryStr  = url.match(/(.+)\?(.+)/)[2];
var urlParams = new URLSearchParams(queryStr);
var params    = {};

for (let item of urlParams.entries())
  params[item[0]] = item[1];
  
console.log(JSON.stringify(params,null,2));
Dee
  • 7,455
  • 6
  • 36
  • 70