0

I have been able to write a python script to get Base 64 auth for my username and password (Admin:password) equal to --> Basic QWRtaW46cGFzc3dvcmQ= When I add that to my header manager as: Authorization Basic QWRtaW46cGFzc3dvcmQ=
all my HTTP Requests succeed.


in Jmeter I have googled and I find to add below in Bean PreProcessor:

import org.apache.commons.codec.binary.Base64;
String username = vars.get("Username");
String password = vars.get("Password");
String combineduserpass = username + ":" + password;
byte[] encodedUsernamePassword =         
Base64.encodeBase64(combineduserpass.getBytes());
vars.put("base64HeaderValue",new String(encodedUsernamePassword));

System.out.println(encodedUsernamePassword);

but that system output gives me --> [B@558e816b which is incorrect

when I add that to my Header manager like this

Authorization Basic ${base64HeaderValue}

my HTTP Req obviously fails. The Base64 for "Admin:password should really be Basic QWRtaW46cGFzc3dvcmQ= and not [B@558e816b

Ghost
  • 549
  • 8
  • 29

3 Answers3

0

You are trying to print byte array. You can print the new variable as:

 System.out.println(vars.get("base64HeaderValue"));

Also your Header Manager should be under your HTTP Request so it be execute aftet script and before your request

Instead of scrpting you can use JMeter plugin of custom functions and use inside Header manager the __base64Encode function similar to:

  ${__base64Encode(test string, base64HeaderValue)}
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
0

To do Basic Auth, just add HTTP Authorization Manager to your plan as per this answer:

It would be configured like this if your server URL is http://localhost:8080/test:

Http Authorization Manager

There is no need for scripting here.

UBIK LOAD PACK
  • 33,980
  • 5
  • 71
  • 116
0

I would recommend switching to JSR223 PreProcessor and Groovy language as:

  1. Groovy supports all modern Java language features including (but not limited to)

  2. Groovy performance is way better comparing to Beanshell

Groovy equivalent of your code would be:

vars.put('base64HeaderValue',(vars.get('Username') + ':' + vars.get('Password')).bytes.encodeBase64().toString())
Dmitri T
  • 159,985
  • 5
  • 83
  • 133