Post JSON using Python Requests

Charles R :

I need to POST a JSON from a client to a server. I'm using Python 2.7.1 and simplejson. The client is using Requests. The server is CherryPy. I can GET a hard-coded JSON from the server (code not shown), but when I try to POST a JSON to the server, I get "400 Bad Request".

Here is my client code:

data = {'sender':   'Alice',
    'receiver': 'Bob',
    'message':  'We did it!'}
data_json = simplejson.dumps(data)
payload = {'json_payload': data_json}
r = requests.post("http://localhost:8080", data=payload)

Here is the server code.

class Root(object):

    def __init__(self, content):
        self.content = content
        print self.content  # this works

    exposed = True

    def GET(self):
        cherrypy.response.headers['Content-Type'] = 'application/json'
        return simplejson.dumps(self.content)

    def POST(self):
        self.content = simplejson.loads(cherrypy.request.body.read())

Any ideas?

Zeyang Lin :

As of Requests version 2.4.2 and onwards, you can alternatively use 'json' parameter in the call which makes it simpler.

>>> import requests
>>> r = requests.post('http://httpbin.org/post', json={"key": "value"})
>>> r.status_code
200
>>> r.json()
{'args': {},
 'data': '{"key": "value"}',
 'files': {},
 'form': {},
 'headers': {'Accept': '*/*',
             'Accept-Encoding': 'gzip, deflate',
             'Connection': 'close',
             'Content-Length': '16',
             'Content-Type': 'application/json',
             'Host': 'httpbin.org',
             'User-Agent': 'python-requests/2.4.3 CPython/3.4.0',
             'X-Request-Id': 'xx-xx-xx'},
 'json': {'key': 'value'},
 'origin': 'x.x.x.x',
 'url': 'http://httpbin.org/post'}

EDIT: This feature has been added to the official documentation. You can view it here: Requests documentation

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Post JSON using Python Requests

Python Requests - post json

how to POST multipart list of JSON/xml files using python requests

flask python requests post using JSON data to localhost

Can I post a .json file on github using python requests?

How to POST JSON data with Python Requests?

Python Requests: Post JSON and file in single request

Python requests post json raw data

Mocking requests.post and requests.json decoder python

Using python requests to POST data with file

Sending images by POST using python requests

Python : Trying to POST form using requests

Python Requests: Logging into website using POST and Cookies

Post request with arrays using python requests

POST request to localhost using Python requests

post multiple images to fastapi using requests python

Using Python Requests to POST data to REST API

Getting requests.exceptions.SSLError when using post in python requests

how to POST contents of JSON file to RESTFUL API with Python using requests module

Python Requests - debugging POST requests

Loading JSON using Python Requests wrong type

How to merge json get requests using Python?

Adding API requests into one JSON using Python

Receiving null arguments in POST requests with JSON body using Spring Boot

Simple Python server to process GET and POST requests with JSON

Python3: JSON POST Request WITHOUT requests library

Python POST requests to an API with JSON data work on Requestbin, but not locally

Python Requests: POST JSON and file (multiform data) in 1 single request

Python requests library can't specify JSON key on HTTP POST