headers['Content-Length'] is None

Question:

I am using Python 3.6.

I’m trying to return JSON to a post request:

from http.server import HTTPServer, SimpleHTTPRequestHandler 
from io import BytesIO

class TaroHttpHandler(SimpleHTTPRequestHandler):
    def parse_url(self, path):
        ...
        return path_component, query_components


    def do_POST(self):

        path_component, _ = self.parse_url(self.path)
        if path_component == '/taro/three-cards/set-rating/':
            content_length = int(self.headers['Content-Length'])
            body = self.rfile.read(content_length)
            self.send_header(keyword="Content-Type", value="application/json")
            self.send_response(200)  # 200 Ok is printed.
            self.end_headers()
            response = BytesIO()
            response.write(b'{Rating: 1}')
            response.write(body)
            self.wfile.write(response.getvalue()) # Breakpoint

def run(server_class=HTTPServer, 
    handler_class=TaroHttpHandler):
    server_address = ('', 8000)
    httpd = server_class(server_address, handler_class)
    httpd.serve_forever()

run()

(Github: https://github.com/Kifsif/tmp.git).

I send a post-request:

curl -H "User-Agent: floor-9" -X POST http://localhost:8000/taro/three-cards/set-rating/
curl: (52) Empty reply from server

At the server side I have an error:

----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 45300)
Traceback (most recent call last):
  File "/usr/lib/python3.6/socketserver.py", line 320, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python3.6/socketserver.py", line 351, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python3.6/socketserver.py", line 364, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python3.6/socketserver.py", line 724, in __init__
    self.handle()
  File "/usr/lib/python3.6/http/server.py", line 418, in handle
    self.handle_one_request()
  File "/usr/lib/python3.6/http/server.py", line 406, in handle_one_request
    method()
  File "/home/michael/Documents/taro/taro.py", line 71, in do_POST
    content_length = int(self.headers['Content-Length'])
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
----------------------------------------

Why does the error exist and how to cope with it? When I send a post request via a Postman program, content_length = 165. In case of curl, self.headers[‘Content-Length’] = None. I even added a user-agent header. But it is still None.

Asked By: Michael

||

Answers:

This should help.

Use the -d flag with and empty string to your curl command, this will attach data of 0 length to the request and thus a content length header will be added.

curl -H "User-Agent: floor-9" -X POST http://localhost:8000/taro/three-cards/set-rating/ -d ""

It may be worth handling the error in a try except though and respond with a 400 Bad Request

Answered By: GTBebbo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.