0

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 .

chepner
  • 497,756
  • 71
  • 530
  • 681

2 Answers2

1

here by example answer:

import json

data = {}
data['people'] = []
data['people'].append({
    'name': 'Larry',
    'website': 'google.com',
    'from': 'Michigan'
})
data['people'].append({
    'name': 'Tim',
    'website': 'apple.com',
    'from': 'Alabama'
})

with open('data.txt', 'w') as outfile:
    json.dump(data, outfile)

You might also try this solution.

p t
  • 11
  • 2
0

thank you , I somehow managed to do this by below code ( literal_eval and json.dumps then write to file as string), however , i am sure there are better ways to do this, i will keep exploring it further. Thank you All.

from http.server import HTTPServer, BaseHTTPRequestHandler
from io import BytesIO
from datetime import datetime
import os.path
from ast import literal_eval
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('\nthis 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())

        data = literal_eval(body.decode('utf8'))
        #s = json.dumps(data, indent=4, sort_keys=True)
        s = json.dumps(data, indent=4, sort_keys=False)
        fi.write(s)
        #json.dump(data,fi)
        
        fi.close()


httpd = HTTPServer(('localhost', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever()
Ron
  • 176
  • 1
  • 10