title: How to fix '415 Unsupported Media Type' error in Python using requests
body: <p>I would like to create a commit by using bitbucket's rest api. So far, all the answers to questions about <code>Response 415</code> have been resolved by setting the <code>Content-Type</code> in the header to <code>application/json;charset-UTF8</code>. However, this does not solve the response I get.</p>
  
  <p>So here is what I am trying to do:</p>
  
  <pre class="lang-py prettyprint-override"><code>import requests
  
  def commit_file(s, path, content, commit_message, branch, source_commit_id):
      data = dict(content=content, message=commit_message, branch=branch, sourceCommitId=source_commit_id)
      r = s.put(path, data=data, headers={'Content-type': 'application/json;charset=utf-8'})
      return r.status_code
  
  s = requests.Session()
  s.auth = ('name', 'token')
  url = 'https://example.com/api/1.0/projects/Project/repos/repo/browse/file.txt'
  file = s.get(url)
  r = commit_file(s, url, file.json() , 'Commit Message', 'test', '51e0f6faf64')
  </code></pre>
  
  <p>The <code>GET</code> requests successfully returns the file and I would like to commit it's contents on the branch <code>test</code> which does exist.</p>
  
  <p>No matter the <code>Content-Type</code>, the <code>status_code</code> of the response is <code>415</code>.</p>
  
  <p>Here is the header of the put request:</p>
  
  <pre><code>OrderedDict([('user-agent', ('User-Agent', 'python-requests/2.21.0')), ('accept-encoding', ('Accept-Encoding', 'gzip, deflate')), ('accept', ('Accept', '*/*')), ('connection', ('Connection', 'keep-alive')), ('content-type', ('Content-type', 'application/json;charset=utf-8')), ('content-length', ('Content-Length', '121')), ('authorization', ('Authorization', 'Basic YnVybWF4MDA6Tnp...NkJqWGp1a2JjQ3dNZzhHeGI='))])
  </code></pre>
  
  <p><a href="https://docs.atlassian.com/bitbucket-server/rest/5.7.0/bitbucket-rest.html#idm45568365784624" rel="nofollow noreferrer">This</a> explains the usage with curl and when the file is locally available. How would a correct request look like in python when the content of the file are retrieved as shown above?</p>
  
  <p>This is the solution by using the <code>MultipartEncoder</code>:</p>
  
  <pre><code>import requests
  import requests_toolbelt.multipart.encoder
  
  def commit_file(s, path, content, commit_message, branch, source_commit_id):
      data = requests_toolbelt.MultipartEncoder(
          fields={
              'content': content,
              'message': commit_message,
              'branch': branch,
              'sourceCommitId': source_commit_id
          }
      )
      r = s.put(path, data=data, headers={'Content-type': data.content_type})
  </code></pre>