I am new to Python and open system technology. I am developing a http server in python which will be receiving a HTTP POST request from a TPF system in JSON format. I am trying to test my python server from POSTMAN where I send sample JSON POST request to the localhost:8000 running on my machine , what I want is to take the JSON POST body and write to a file.
I have below code which will read the JSON POST body and send back the same response to the client however I am not able to figure out how do I dump same response to the file provided by user:
from http.server import HTTPServer, BaseHTTPRequestHandler
from io import BytesIO
from datetime import datetime
import os.path
import json
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
    def do_POST(self):
        print('this is PNR compare service Response')
        infi = input("enter the name of the file (.json extension) to save  compare result : ")
        infi = os.path.normpath(infi)
        fi = open(infi,'a+')
        now = datetime.now()
        dt_string = now.strftime(" %d/%m/%Y %H:%M:%S\n")
        fi.write('this is PNR compare service Response at' + dt_string)
                
        content_length = int(self.headers['Content-Length'])
        body = self.rfile.read(content_length)
        self.send_response(200)
        self.end_headers()      
        response = BytesIO()
        response.write(b'This is POST request. ')
        response.write(b'Received: ')
        response.write(body)
        self.wfile.write(response.getvalue())
        #json.dump(body,fi)  
        #json.dump(response.getvalue(),fi)
        #json.dump(response.getvalue(),fi)
        fi.close()
httpd = HTTPServer(('localhost', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever()
Since my response is Byte format, json.dump fails/takes exception (Object of type bytes is not JSON serializable) because it expects a JSON string. Is there any simple way to directly get JSON body from POST request ? My post request is going to be extremely heavy and will have very large JSON body, so looking for a efficient way to parse and dump the response into the file .
 
     
     
    