0

currently i am setting as below:

connection.setRequestProperty("Authorization", "Basic NDUwNjMyOTc6U2hyaXNo2weQDEy"); 

Basic NDUwNjMyOTc6U2hyaXNo2weQDEy"----------This part i have generated using postman. but it contains user name and password.

How to generate this Auth Basic NDUwNjMyOTc6U2hyaXNo2weQDEy using java and pass it in setRequestProperty.

Thanks in advance :)

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Routray
  • 75
  • 1
  • 12

2 Answers2

3

The basic auth header is generated from username:password using base 64, the header authorization value could be generated like so:

Encoder encoder = Base64.getEncoder();
String originalString = username+":"+password;
String encodedString = encoder.encodeToString(originalString.getBytes());

String headerAthorization="Basic "+encodedString;
Ismail
  • 2,322
  • 1
  • 12
  • 26
  • @M.Ismain have update as below but still its not working: Encoder encoder = Base64.getEncoder(); String originalString = name+":"+password; String encodedString = encoder.encodeToString(originalString.getBytes()); String headerAthorization="basic "+encodedString; connection.setRequestProperty("Authorization", headerAthorization); – Routray Aug 17 '20 at 16:55
  • @Routray what is the error exactly? the word basic should not be case-sensitive, try with Basic instead of basic. – Ismail Aug 18 '20 at 08:39
  • @Ismail, thanks for identifying my mistake...now its working fine – Routray Aug 19 '20 at 11:04
0

You add header Basic ENCODED, where the encoded is base64 of "user:password". See https://github.com/jcabi/jcabi-http/blob/master/src/main/java/com/jcabi/http/wire/BasicAuthWire.java#L123 for example.

andreoss
  • 1,570
  • 1
  • 10
  • 25