# Buggy code
def _on_request_body_finish(self):
    """
    Called when the request body has been read completely. Takes care of creating the replacement body out of the
    logged parts, turning ``file`` parts into new ``data`` parts.
    """

    self._new_body = b""
    for name, part in self._parts.items():
        if "filename" in part:
            # add form fields for filename, path, size and content_type for all files contained in the request
            if not "path" in part:
                continue

            parameters = dict(
                name=part["filename"],
                path=part["path"],
                size=str(os.stat(part["path"]).st_size),
            )
            if "content_type" in part:
                parameters["content_type"] = part["content_type"]

            fields = dict(
                (self._suffixes[key], value) for (key, value) in parameters.items()
            )
            for n, p in fields.items():
                key = name + b"." + octoprint.util.to_bytes(n)
                self._new_body += b"--%s\r\n" % self._multipart_boundary
                self._new_body += b'Content-Disposition: form-data; name="%s"\r\n' % key
                self._new_body += b"Content-Type: text/plain; charset=utf-8\r\n"
                self._new_body += b"\r\n"
                self._new_body += octoprint.util.to_bytes(p) + b"\r\n"
        elif "data" in part:
            self._new_body += b"--%s\r\n" % self._multipart_boundary
            value = part["data"]
            self._new_body += b'Content-Disposition: form-data; name="%s"\r\n' % name
            if "content_type" in part and part["content_type"] is not None:
                self._new_body += b"Content-Type: %s\r\n" % part["content_type"]
            self._new_body += b"\r\n"
            self._new_body += value + b"\r\n"
    self._new_body += b"--%s--\r\n" % self._multipart_boundary


# Traceback (TypeError)
2019-11-25 17:09:34,089 - tornado.application - ERROR - Uncaught exception
Traceback (most recent call last):
File "/home/pi/oprint/local/lib/python2.7/site-packages/tornado/http1connection.py", line 584, in _read_fixed_body
ret = delegate.data_received(body)
File "/home/pi/oprint/local/lib/python2.7/site-packages/tornado/httpserver.py", line 311, in data_received
return self.delegate.data_received(chunk)
File "/home/pi/oprint/local/lib/python2.7/site-packages/tornado/routing.py", line 248, in data_received
return self.delegate.data_received(chunk)
File "/home/pi/oprint/local/lib/python2.7/site-packages/tornado/web.py", line 2087, in data_received
return self.handler.data_received(data)
File "/home/pi/oprint/local/lib/python2.7/site-packages/octoprint/server/util/tornado.py", line 271, in data_received
self._process_multipart_data(data)
File "/home/pi/oprint/local/lib/python2.7/site-packages/octoprint/server/util/tornado.py", line 316, in _process_multipart_data
self._on_request_body_finish()
File "/home/pi/oprint/local/lib/python2.7/site-packages/octoprint/server/util/tornado.py", line 468, in _on_request_body_finish
self._new_body += octoprint.util.to_bytes(p) + b'\r\n'
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

# Fixed code
def _on_request_body_finish(self):
    """
    Called when the request body has been read completely. Takes care of creating the replacement body out of the
    logged parts, turning ``file`` parts into new ``data`` parts.
    """

    self._new_body = b""
    for name, part in self._parts.items():
        if "filename" in part:
            # add form fields for filename, path, size and content_type for all files contained in the request
            if not "path" in part:
                continue

            parameters = dict(
                name=part["filename"],
                path=part["path"],
                size=str(os.stat(part["path"]).st_size),
            )
            if "content_type" in part:
                parameters["content_type"] = part["content_type"]

            fields = dict(
                (self._suffixes[key], value) for (key, value) in parameters.items()
            )
            for n, p in fields.items():
                if n is None or p is None:
                    continue
                key = name + b"." + octoprint.util.to_bytes(n)
                self._new_body += b"--%s\r\n" % self._multipart_boundary
                self._new_body += b'Content-Disposition: form-data; name="%s"\r\n' % key
                self._new_body += b"Content-Type: text/plain; charset=utf-8\r\n"
                self._new_body += b"\r\n"
                self._new_body += octoprint.util.to_bytes(p) + b"\r\n"
        elif "data" in part:
            self._new_body += b"--%s\r\n" % self._multipart_boundary
            value = part["data"]
            self._new_body += b'Content-Disposition: form-data; name="%s"\r\n' % name
            if "content_type" in part and part["content_type"] is not None:
                self._new_body += b"Content-Type: %s\r\n" % part["content_type"]
            self._new_body += b"\r\n"
            self._new_body += value + b"\r\n"
    self._new_body += b"--%s--\r\n" % self._multipart_boundary