Python BaseHTTPRequestHandler: Respond with JSON
Asked Answered
D

3

11

I have a Python class that inherits BaseHTTPRequestHandler and implements the method do_POST.

I currently only succeed to respond with an integer status, e.g. 200, using the following command at the end of the method:

self.send_response(200)

I am trying to also send some string as a part of the response. How should I do it?

Daric answered 2/1, 2017 at 15:9 Comment(0)
D
9

It turns out to be pretty simple, though there aren't many examples for it.

Just use:

self.wfile.write(YOUR_STRING_HERE)

Specifically for the case of json:

import json
json_string = json.dumps(YOUR_DATA_STRUCTURE_TO_CONVERT_TO_JSON)
self.wfile.write(json_string)
Daric answered 2/1, 2017 at 15:12 Comment(3)
This does not work, write expects a binary string and json.dumps outputs a normal string. You need to wrap it in bytes(json.dumps(ASDASDF), 'utf-8')Alodi
Probably a Python2/3 compatibility problemDaric
@Alodi use self.wfile.write(json_string.encode(encoding='utf_8')Fennel
W
25

At least in my environment (Python 3.7) i have to use

self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(json_str.encode(encoding='utf_8'))

otherwise this error will be thrown: TypeError: a bytes-like object is required, not 'str'

Weidner answered 25/2, 2019 at 16:9 Comment(1)
or you could use bytes like this: self.wfile.write(bytes(json.dumps(object, ensure_ascii=False), 'utf-8'))Yepez
D
9

It turns out to be pretty simple, though there aren't many examples for it.

Just use:

self.wfile.write(YOUR_STRING_HERE)

Specifically for the case of json:

import json
json_string = json.dumps(YOUR_DATA_STRUCTURE_TO_CONVERT_TO_JSON)
self.wfile.write(json_string)
Daric answered 2/1, 2017 at 15:12 Comment(3)
This does not work, write expects a binary string and json.dumps outputs a normal string. You need to wrap it in bytes(json.dumps(ASDASDF), 'utf-8')Alodi
Probably a Python2/3 compatibility problemDaric
@Alodi use self.wfile.write(json_string.encode(encoding='utf_8')Fennel
M
3

It's an old question. Still, if someone else might be wondering the same, here's my 2 cent.

If you are doing anything useful, apart from playing around with python, you should start looking for standard python frameworks to handle HTTP server operations, like Django or Flask.

That being said, there's a small stub that I use to act as a test server for my outgoing requests, which should answer your question. You can set any status code, header or response body by modifying it:

#!/usr/bin/env python
# Reflects the requests with dummy responses from HTTP methods GET, POST, PUT, and DELETE
# Written by Tushar Dwivedi (2017)

import json
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from optparse import OptionParser

class RequestHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        request_path = self.path

        print("\n----- Request Start ----->\n")
        print("request_path :", request_path)
        print("self.headers :", self.headers)
        print("<----- Request End -----\n")

        self.send_response(200)
        self.send_header("Set-Cookie", "foo=bar")
        self.end_headers()
        self.wfile.write(json.dumps({'hello': 'world', 'received': 'ok'}))

    def do_POST(self):
        request_path = self.path

        # print("\n----- Request Start ----->\n")
        print("request_path : %s", request_path)

        request_headers = self.headers
        content_length = request_headers.getheaders('content-length')
        length = int(content_length[0]) if content_length else 0

        # print("length :", length)

        print("request_headers : %s" % request_headers)
        print("content : %s" % self.rfile.read(length))
        # print("<----- Request End -----\n")

        self.send_response(200)
        self.send_header("Set-Cookie", "foo=bar")
        self.end_headers()
        self.wfile.write(json.dumps({'hello': 'world', 'received': 'ok'}))

    do_PUT = do_POST
    do_DELETE = do_GET


def main():
    port = 8082
    print('Listening on localhost:%s' % port)
    server = HTTPServer(('', port), RequestHandler)
    server.serve_forever()


if __name__ == "__main__":
    parser = OptionParser()
    parser.usage = ("Creates an http-server that will echo out any GET or POST parameters, and respond with dummy data\n"
                    "Run:\n\n")
    (options, args) = parser.parse_args()

    main()

Again, even if you are just learning, and you even need to add 5-6 of if elses to the above to do what you are doing, it's better to do things right from the beginning, to avoid a lot of rework in future. Use a framework capable of handling boilerplate stuff for you.

Mario answered 8/9, 2018 at 11:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.