Sending a JSON string as a post request

23,660

Since I have a similar application running, but the client still was missing, I tried it myself. The server which is running is from the following exercise:

Miguel Grinberg - designing a restful API using Flask

That's why it uses authentication.

But the interesting part: Using requests you can leave the dictionary as it is.

Look at this:

username = 'miguel'
password = 'python'

import requests
content = {"title":"Read a book"}

request = requests.get("http://127.0.0.1:5000/api/v1.0/projects", auth=(username, password), params=content)
print request.text

It seems to work :)

Update 1:

POST requests are done using requests.post(...) This here describes it well : python requests

Update 2:

In order to complete the answer:

requests.post("http://127.0.0.1:5000/api/v1.0/projects", json=content)

sends the json-string.

json is a valid parameter of the request and internally uses json.dumps()...

Share:
23,660
Quad
Author by

Quad

Updated on July 09, 2022

Comments

  • Quad
    Quad almost 2 years

    rocksteady's solution worked

    He did originally refer to dictionaries. But the following code to send the JSON string also worked wonders using requests:

    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)
    

    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.

    The following Python 2 code works fine, in fact it's the official sample:

    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)
    

    However, using this code in Python 3 only makes it complain about data being invalid:

    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)
    

    I've tried doing urlencode(), 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.

    EDIT: As requested, here are the errors I get. Since this is a flask application, here's a screenshot of the debugger:

    Screenshot

    Adding .encode('utf-8') gives me an "Expected string or buffer" error

    EDIT 2: Screenshot of the debugger with .encode('utf-8') added

  • Quad
    Quad over 8 years
    Dictionaries aren't the issue. Those work fine, I'm having trouble sending a single JSON string that is not part of any dictionary or list. But can test requests with a string as well, since you suggested it.