Skip to content

Instantly share code, notes, and snippets.

@brandonmwest
Last active January 16, 2024 15:52
Star You must be signed in to star a gist
Save brandonmwest/a2632d0a65088a20c00a to your computer and use it in GitHub Desktop.
Generating base64-encoded Authorization headers in a variety of languages
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", username, password))));
$header = "Authorization: Basic " . base64_encode($username . ':' . $password);
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
header = ("Authorization: Basic %s" % base64string)
$header = 'Authorization: Basic ' + Base64.encode64( username + ':' + password ).chomp
$.ajax
({
type: "GET",
url: "https://www.example.com",
dataType: 'json',
headers: {
"Authorization", btoa(username + ":" + password)
},
data: '{}',
success: function (){
...
}
});
var username = 'Test';
var password = '123';
var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');
var header = {'Host': 'www.example.com', 'Authorization': auth};
var request = client.request('GET', '/', header);
@holgerdewall
Copy link

holgerdewall commented Aug 8, 2023

In oracle pl/sql

set serveroutput on;
DECLARE
v_str VARCHAR2(1000);
BEGIN
--Create encoded value
v_str := utl_encode.text_encode('Testuser:verySecret','WE8ISO8859P1', UTL_ENCODE.BASE64);
dbms_output.put_line(v_str);
--Decode the value..
v_str := utl_encode.text_decode(v_str,'WE8ISO8859P1', UTL_ENCODE.BASE64);
dbms_output.put_line(v_str);
END;
/

or

select utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw('Testuser:verySecret')))
  from dual;
/

@suaterken-ua
Copy link

in JQuery, aren't we missing the 'Basic ' prefix?

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment