title: Basic Auth urllib2 with HTTPPasswordMgrWithDefaultRealm and POST data
body: <p>I have this cURL call that works perfectly:</p>
  
  <pre><code>curl -H 'X-Requested-With: SO demo' -d 'parameter=value' https://username:password@api.domain.com/api/work/
  </code></pre>
  
  <p>My conversion does not work.  </p>
  
  <pre><code>import urllib2
  # Create a password manager.
  password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
  # Add the username and password.
  top_level_url = 'https://api.server.com'
  password_mgr.add_password(None, top_level_url, 'username', 'password')
  handler = urllib2.HTTPBasicAuthHandler(password_mgr)
  # Create "opener" (OpenerDirector instance).
  opener = urllib2.build_opener(handler)
  # Install the opener so all calls to urllib2.urlopen use our opener.
  urllib2.install_opener(opener)
  # Create request.
  headers = {'X-Requested-With':'SO demo.'}
  uri = 'https://api.domain.com/api/work/'
  data='parameter=value'
  req = urllib2.Request(uri,data,headers)
  # Make request to fetch url.
  result = urllib2.urlopen(req)
  urllib2.HTTPError: HTTP Error 401: Unauthorized
  </code></pre>
  
  <p>Here's what I don't get.  The same server has a separate API which similar code does work on, where the only thing that has changed is the parameter and uri.  Note the cURL call works on both API calls.  </p>
  
  <p>Second API cURL call (that works):</p>
  
  <pre><code>curl -H 'X-Requested-With: SO demo' -d 'parameter=value' https://username:password@api.domain.com/api2/call.php
  </code></pre>
  
  <p>Equivalent code that works below:</p>
  
  <pre><code>import urllib2
  # Create a password manager.
  password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
  # Add the username and password.
  top_level_url = 'https://api.server.com'
  password_mgr.add_password(None, top_level_url, 'username', 'password')
  handler = urllib2.HTTPBasicAuthHandler(password_mgr)
  # Create "opener" (OpenerDirector instance).
  opener = urllib2.build_opener(handler)
  # Install the opener.
  # Now all calls to urllib2.urlopen use our opener.
  urllib2.install_opener(opener)
  # Create request.
  headers = {'X-Requested-With':'SO demo.'}
  uri = 'https://api.server.com/api2/call.php'
  data='parameter=value'
  req = urllib2.Request(uri,data,headers)
  # Make request to fetch url.
  result = urllib2.urlopen(req)
  # Read results.
  result.read()
  </code></pre>
  
  <p>Why does urllib2 work when the uri ends with a '.php', but <strong>not work</strong> when the uri ends with a '/'?</p>