title: Postman creates different base64 value than Python's base64 module for unicode passwords - what explains the difference?
body: <p>I created a random password in python using <code>os.urandom(10)</code> which creates a password with unicode values; for example:</p>
  
  <pre><code>&gt;&gt;&gt; import os
  &gt;&gt;&gt; os.urandom(10)
  '\x8c[A\x91\xf0dq\xb2{\xcc'
  </code></pre>
  
  <p>In my company's ldap tool, I set a user's password as that value.</p>
  
  <p>In Postman, I can create the Basic Authorization header by copy and pasting that value, and <strong>successfully authenticate</strong>.</p>
  
  <p>I tried <code>requests.HTTPBasicAuth</code>, but it failled with a <code>UnicodeDecodeError</code> From another question, I learned that HTTPBasicAuth doesn't handle unicode values, intentionally. So I tried to do it myself with the base64 module:</p>
  
  <pre><code>import requests
  import base64
  ...
  encoded = base64.b64encode(username + ':' + password)
  headers = {'Authorization': 'Basic ' + encoded}
  r = requests.post(end_point, headers=headers, verify=False)
  </code></pre>
  
  <p>This however <strong>results in a different base64 encoded value</strong> that the one that Postman builds. The request fails because of authorization.</p>
  
  <p>Additionally, if I copy and paste the base64 encoded value that Postman created into the code, the requests works and I am authorized successfully. This tells me that Postman is successfully doing it.</p>
  
  <hr>
  
  <p>To replicate this yourself, open Postman and create a Basic Auth using username "username" and password "\x8c[A\x91\xf0dq\xb2{\xcc". Postman generates the following 64 encoded string:</p>
  
  <pre><code>dXNlcm5hbWU6XHg4Y1tBXHg5MVx4ZjBkcVx4YjJ7XHhjYw==
  </code></pre>
  
  <p>In python, it does the following instead:</p>
  
  <pre><code>&gt;&gt;&gt; import base64
  &gt;&gt;&gt; username = 'username'
  &gt;&gt;&gt; password = '\x8c[A\x91\xf0dq\xb2{\xcc'
  &gt;&gt;&gt; base64.b64encode(username + ':' + password)
  'dXNlcm5hbWU6jFtBkfBkcbJ7zA=='
  </code></pre>
  
  <p>What accounts for these differences, and how can I get python to generate the same value that Postman does (as Postman generates the correct value?)</p>