1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How can I POST JSON data with Python's Requests library?

Discussão em 'Python' iniciado por Stack, Novembro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I need to POST JSON content 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 hard-coded JSON content from the server (the code is not shown), but when I try to POST JSON content 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())


    How can I fix this problem?

    Continue reading...

Compartilhe esta Página