title: Sending a JSON string as a post request
body: <p><strong>rocksteady's solution worked</strong></p>
  
  <p>He did originally refer to dictionaries. But the following code to send the JSON string also worked wonders using requests:</p>
  
  <pre><code>import requests
  
  headers = {
    'Authorization': app_token
  }
  url = api_url + "/b2api/v1/b2_get_upload_url"
  content = json.dumps({'bucketId': bucket_id})
  
  r = requests.post(url, data = content, headers = headers)
  </code></pre>
  
  <hr>
  
  <p>I'm working with an API that requires me to send JSON as a POST request to get results. Problem is that Python 3 won't allow me to do this.</p>
  
  <p>The following Python 2 code works fine, in fact it's the official sample:</p>
  
  <pre><code>request = urllib2.Request(
      api_url +'/b2api/v1/b2_get_upload_url',
      json.dumps({ 'bucketId' : bucket_id }),
      headers = { 'Authorization': account_authorization_token }
  )
  response = urllib2.urlopen(request)
  </code></pre>
  
  <p>However, using this code in Python 3 only makes it complain about data being invalid:</p>
  
  <pre><code>import json
  from urllib.request import Request, urlopen
  from urllib.parse import urlencode
  
  # -! Irrelevant code has been cut out !-
  
  headers = {
    'Authorization': app_token
  }
  url = api_url + "/b2api/v1/b2_get_upload_url"
  
  # Tested both with encode and without
  content = json.dumps({'bucketId': bucket_id}).encode('utf-8')
  
  request = Request(
    url=url,
    data=content,
    headers=headers
  )
  
  response = urlopen(req)
  </code></pre>
  
  <p>I've tried doing <code>urlencode()</code>, like you're supposed to. But this returns a 400 status code from the web server, because it's expecting pure JSON. Even if the pure JSON data is invalid, I need to somehow force Python into sending it.</p>
  
  <p><strong>EDIT</strong>: As requested, here are the errors I get. Since this is a flask application, here's a screenshot of the debugger:</p>
  
  <p><a href="https://chitoge.quad.moe/storage/sharex/2015-12-22_15-10-13.png" rel="nofollow">Screenshot</a></p>
  
  <p>Adding <code>.encode('utf-8')</code> gives me an "Expected string or buffer" error</p>
  
  <p><strong>EDIT 2</strong>: <a href="https://chitoge.quad.moe/storage/sharex/2015-12-22_15-30-02.png" rel="nofollow">Screenshot</a> of the debugger with <code>.encode('utf-8')</code> added</p>