简体   繁体   中英

How to preserve newlines and whitespaces in Jmeter parameters

在此处输入图像描述 Hi i need to add a piece of code to my Jmeter parameters for my request as follows:

#include <string>
#include <vector>
#include <iostream>

using namespace std; 

int main() 
{ 
//use cin to get console input 
string test;
getline(cin,test);

vector<char> newstr;
for (int i = 0; i< test.size();i++)
{
char c = test[i];
if ((c>='a'&& c <='z') || (c>='A'&& c <='Z'))
{
    //cout<<"c: "<<c<<endl;
    newstr.push_back(tolower(c));
}
}

if (newstr.size()==0)
{
    cout<<"YES"<<endl;
        return 0;
    }
for (int i = 0; i< newstr.size()/2;i++)
{
char a = newstr[i];
char b = newstr[newstr.size()-1-i];
if (a != b)
{
    //cout<<newstr[i]<<endl;
    cout<<"NO"<<endl;
    return 0;
}

}
cout<<"YES"<<endl; 

//use cout to print console output 

return 0; 
}

However, when i add it as a parameter, it takes the entire code in a single line and never executes it. ie it takes it as follows:

#include <string> #include <vector> #include <iostream>  using namespace std;   int main()  {  //use cin to get console input  string test; getline(cin,test);  vector<char> newstr; for (int i = 0; i< test.size();i++) { char c = test[i]; if ((c>='a'&& c <='z') || (c>='A'&& c <='Z')) {    //cout<<"c: "<<c<<endl;     newstr.push_back(tolower(c)); } }  if (newstr.size()==0) {  cout<<"YES"<<endl;      return 0;   } for (int i = 0; i< newstr.size()/2;i++) { char a = newstr[i]; char b = newstr[newstr.size()-1-i]; if (a != b) {   //cout<<newstr[i]<<endl;    cout<<"NO"<<endl;   return 0; }  } cout<<"YES"<<endl;   //use cout to print console output   return 0;  } 

and never executes the code.

Also, the code works fine in postman as it accepts whitespaces and newline characters..

I tried it using the escape sequence but it doesn't work for me . Is there any way out?

The parameters are as follows:

requestid:2
store:${hdn_store}
txtsol: #include <string>
#include <vector>
#include <iostream>

using namespace std; 

int main() 
{ 
//use cin to get console input 
string test;
getline(cin,test);

vector<char> newstr;
for (int i = 0; i< test.size();i++)
{
char c = test[i];
if ((c>='a'&& c <='z') || (c>='A'&& c <='Z'))
{
    //cout<<"c: "<<c<<endl;
    newstr.push_back(tolower(c));
}
}

if (newstr.size()==0)
{
    cout<<"YES"<<endl;
        return 0;
    }
for (int i = 0; i< newstr.size()/2;i++)
{
char a = newstr[i];
char b = newstr[newstr.size()-1-i];
if (a != b)
{
    //cout<<newstr[i]<<endl;
    cout<<"NO"<<endl;
    return 0;
}

}
cout<<"YES"<<endl; 

//use cout to print console output 

return 0; 
}

langcode:1

There are different approaches, for example:

  1. Store your code into the file and use __FileToString() function to read it from the file system

  2. Convert the code into Base64 format , it will result in a single line like:

    I2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGlvc3RyZWFtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsgCgppbnQgbWFpbigpIAp7IAovL3VzZSBjaW4gdG8gZ2V0IGNvbnNvbGUgaW5wdXQgCnN0cmluZyB0ZXN0OwpnZXRsaW5lKGNpbix0ZXN0KTsKCnZlY3RvcjxjaGFyPiBuZXdzdHI7CmZvciAoaW50IGkgPSAwOyBpPCB0ZXN0LnNpemUoKTtpKyspCnsKY2hhciBjID0gdGVzdFtpXTsKaWYgKChjPj0nYScmJiBjIDw9J3onKSB8fCAoYz49J0EnJiYgYyA8PSdaJykpCnsKICAgIC8vY291dDw8ImM6ICI8PGM8PGVuZGw7CiAgICBuZXdzdHIucHVzaF9iYWNrKHRvbG93ZXIoYykpOwp9Cn0KCmlmIChuZXdzdHIuc2l6ZSgpPT0wKQp7CiAgICBjb3V0PDwiWUVTIjw8ZW5kbDsKICAgICAgICByZXR1cm4gMDsKICAgIH0KZm9yIChpbnQgaSA9IDA7IGk8IG5ld3N0ci5zaXplKCkvMjtpKyspCnsKY2hhciBhID0gbmV3c3RyW2ldOwpjaGFyIGIgPSBuZXdzdHJbbmV3c3RyLnNpemUoKS0xLWldOwppZiAoYSAhPSBiKQp7CiAgICAvL2NvdXQ8PG5ld3N0cltpXTw8ZW5kbDsKICAgIGNvdXQ8PCJOTyI8PGVuZGw7CiAgICByZXR1cm4gMDsKfQoKfQpjb3V0PDwiWUVTIjw8ZW5kbDsgCgovL3VzZSBjb3V0IHRvIHByaW50IGNvbnNvbGUgb3V0cHV0IAoKcmV0dXJuIDA7IAp9

and use __base64Decode() function to convert it back in the HTTP Request sampler parameters section

Check out Apache JMeter Functions - An Introduction article to learn more about JMeter Functions concept

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM