0
votes

I'm running Performance test in JMeter where I have to pass Authorization details using Header Manager.

Here is my code:

String headerName = "Authorization";
String headerValue = "Basic MyKey MyValue";

Header bcHeader = new Header(headerName,headerValue);
HeaderManager hm = new HeaderManager();
hm.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName());
hm.add(bcHeader);
hm.add(new Header("Content-Type", "application/json"));
hm.add(new Header("Access-Control-Allow-Origin", "*"));

And I'm facing UnAuthorized error.

Please let me know if there is another way to write the code.

Thanks.

2

2 Answers

0
votes

Normally you should be using HTTP Authorization Manager in order to bypass Basic HTTP Auth challenge.

However if you're going to manually construct Authorization header be aware that it should have the following form

  • Name: Authorization
  • Value: Basic

After Basic you need to provide username and password separated by colon and encoded into Base64. So if your username is MyKey and password is MyValue you should encode the string MyKey:MyValue and add the result to the header so it would look like:

Basic TXlLZXk6TXlWYWx1ZQ==

When it comes to Java code it would be something like:

String headerName = "Authorization";
String username = "MyKey";
String password = "MyValue";
Header bcHeader = new Header(headerName,
        "Basic " +
                Base64.encodeBase64String((username + ":" + password).getBytes(StandardCharsets.UTF_8)));
HeaderManager hm = new HeaderManager();
hm.add(bcHeader);
hm.add(new Header("Content-Type", "application/json"));
hm.add(new Header("Access-Control-Allow-Origin", "*"));
hm.setName(JMeterUtils.getResString("header_manager_title"));
hm.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName());
hm.setProperty(TestElement.GUI_CLASS, HeaderPanel.class.getName());
0
votes
  1. If you are getting authorization header value somewhere in your response you can extract and dynamic values and apply correlations to the script.
  2. Here is what you have to do. Extract Authorization header value using regular expressions and store it in a jmeter variable lets assuem you have saved it as Auth .
  3. Add a header manager (Right click on thread group -->config element--> Header manager) according to jmeter scoping rules.
  4. Use ${variablename} and replace the hard coded header value with ${variablename} ,as we saved it in Auth variable you can use ${Auth}.
  5. You can add headers to header manager click on add and give header name and value as shown below

i'm getting authorization value in request 1's reponse as shown below

enter image description here

so to extract this add a regular expression extractor to the same request (request 1) as shown below.

enter image description here

Now we can use ${Auth} in header manager , add header manager to request 2 and give header name and values as shown below

enter image description here

you can see in the results authorization has passed its value enter image description here

For more information on Extracting variables please follow this link

Let me know if it helps