You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@jmeter.apache.org by Ivan Rancati <iv...@sharpmind.de> on 2007/07/10 15:28:00 UTC

Variable substitution in POSTed files

I am using nightly build r554518 (2.2 seems to always POST as multipart,
which the application server I am using rejects) to send a POST request
where I am also sending a file ("Send a File With the Request")

Is there a way to use variable substitution in the file? I guess not, but
perhaps I am missing something obvious.

Thank you,
Ivan
-- 
View this message in context: http://www.nabble.com/Variable-substitution-in-POSTed-files-tf4055837.html#a11520870
Sent from the JMeter - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org


Re: Variable substitution in POSTed files

Posted by sebb <se...@gmail.com>.
On 10/07/07, Ivan Rancati <iv...@sharpmind.de> wrote:
>
>
> sebb-2 wrote:
> >
> > On 10/07/07, Ivan Rancati <iv...@sharpmind.de> wrote:
> >
> > Not within JMeter.
> >
> > Though could use the BeanShell pre-processor to fixup the file if you
> > are prepared to write the necessary Java code.
> >
> >
>
> Thanks for the suggestion. It works well, with one small problem, which I am
> sure depends on my misunderstanding of the BeanShell regex.

Actually that's Java regex.

> In the file to be processed, I can't specify the variables with the
> ${variablename} JMeter syntax, which causes BeanShell syntax errors when the
> regex is compiled, regardless of how I escape the dollar sign (with zero,
> one, two, four backslashes)

The { and } are also meta-characters - repeat count - so you need to
escape those.

And if you define the script in the GUI panel, you need to beware of
JMeter variable replacement which occurs before the script is seen by
BeanShell. Best to use an init file in which you can define a suitable
method. You can pass in variables.

You can of course use any other variable marker characters if you
prefer, but staying with the JMeter syntax might prove useful if
variable replacement is ever added as an enhancement...

> Here is some quick and dirty code, in case anybody else needs it.
>
> infile = "/u/ivan/svn/addCustomer.template";
> outfile = "/u/ivan/svn/addCustomer.xml";
> codepage="utf-8";
> //this is the placeholder to look for in the file. If you get it to work
> with ${USERNAME} let me know
> rep_from = "USERNAME";
> //this is the User Defined Variable in JMeter
> rep_to = vars.get("username");
>
> import java.util.regex.*;
>
> Pattern pattern;
> Matcher matcher;
>
> pattern = Pattern.compile(rep_from);
>
> in_file = new FileInputStream(infile);
> in_r = new InputStreamReader(in_file,codepage);
> in = new BufferedReader(in_r);
> out_file = new FileOutputStream(outfile);
> out_w = new OutputStreamWriter(out_file,codepage);
> out = new PrintWriter(out_w);
>
> StringBuffer sbr  = new StringBuffer();
> while(in.ready()){
>  str = in.readLine();
>  matcher = pattern.matcher(str);
>  out.println(matcher.replaceAll(rep_to));
> }
> in.close();
> out.close();
> --
> View this message in context: http://www.nabble.com/Variable-substitution-in-POSTed-files-tf4055837.html#a11524588
> Sent from the JMeter - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org


Re: Variable substitution in POSTed files

Posted by Ivan Rancati <iv...@sharpmind.de>.

sebb-2 wrote:
> 
> On 10/07/07, Ivan Rancati <iv...@sharpmind.de> wrote:
> 
> Not within JMeter.
> 
> Though could use the BeanShell pre-processor to fixup the file if you
> are prepared to write the necessary Java code.
> 
> 

Thanks for the suggestion. It works well, with one small problem, which I am
sure depends on my misunderstanding of the BeanShell regex.
In the file to be processed, I can't specify the variables with the
${variablename} JMeter syntax, which causes BeanShell syntax errors when the
regex is compiled, regardless of how I escape the dollar sign (with zero,
one, two, four backslashes)

Here is some quick and dirty code, in case anybody else needs it. 

infile = "/u/ivan/svn/addCustomer.template";
outfile = "/u/ivan/svn/addCustomer.xml";
codepage="utf-8";
//this is the placeholder to look for in the file. If you get it to work
with ${USERNAME} let me know
rep_from = "USERNAME";
//this is the User Defined Variable in JMeter
rep_to = vars.get("username");

import java.util.regex.*;

Pattern pattern;
Matcher matcher;

pattern = Pattern.compile(rep_from);

in_file = new FileInputStream(infile);
in_r = new InputStreamReader(in_file,codepage);
in = new BufferedReader(in_r);
out_file = new FileOutputStream(outfile);
out_w = new OutputStreamWriter(out_file,codepage);
out = new PrintWriter(out_w);

StringBuffer sbr  = new StringBuffer();
while(in.ready()){
  str = in.readLine();
  matcher = pattern.matcher(str);
  out.println(matcher.replaceAll(rep_to));
}
in.close();
out.close();
-- 
View this message in context: http://www.nabble.com/Variable-substitution-in-POSTed-files-tf4055837.html#a11524588
Sent from the JMeter - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org


Re: Variable substitution in POSTed files

Posted by sebb <se...@gmail.com>.
On 10/07/07, Ivan Rancati <iv...@sharpmind.de> wrote:
>
> I am using nightly build r554518 (2.2 seems to always POST as multipart,
> which the application server I am using rejects) to send a POST request
> where I am also sending a file ("Send a File With the Request")
>
> Is there a way to use variable substitution in the file? I guess not, but
> perhaps I am missing something obvious.

Not within JMeter.

Though could use the BeanShell pre-processor to fixup the file if you
are prepared to write the necessary Java code.

> Thank you,
> Ivan
> --
> View this message in context: http://www.nabble.com/Variable-substitution-in-POSTed-files-tf4055837.html#a11520870
> Sent from the JMeter - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org