2
votes

When i run a thread group Loop Count with 2 for the first loop it works well but for second iteration it fails because in Header it add two times the Authorization Bearer token

I have created HTTP Request sampler named as “Login Request" inside of this sampler i added BeanShell PreProcessor’

import org.apache.jmeter.protocol.http.control.Header;
sampler.getHeaderManager().add(new Header("Authorization","Bearer " + vars.get("Token")));

It works well for 1 iteration but in second iteration it fails error shows there are two authorization in header

2
Cant you put a condition there checking if the header is already contained in the header manager ? - Yassin Hajaj
Hi @ Yassin Hajaj ... if i am able to put that condition why would i ask the question ? - Parth Tiwari
Did you explain that you tried that in your question? Do you have a fully reproducible exemple? Maybe you should be nicer to people trying to help - Yassin Hajaj
After a small lookup its totally possible to put a condition there to check. Maybe you should Google for a few minutes before dropping a question onnthe site - Yassin Hajaj

2 Answers

3
votes

You don’t need any Beanshell to do that

Just add a Header Manager and put in it :

  • Name: Authorization
  • Value: Bearer ${Token}
1
votes

Amend your code to invoke removeHeaderNamed() function like:

import org.apache.jmeter.protocol.http.control.Header;
sampler.getHeaderManager().removeHeaderNamed("Authorization")
sampler.getHeaderManager().add(new Header("Authorization","Bearer " + vars.get("Token")));

this way you will get confidence that each time you're having only one Authorization header.

Also be aware that since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting mainly because Groovy has much better performance comparing to Beanshell so consider migrating on next available opportunity (the same code will work fine)