# Buggy code
def render_PUT(self, request):
    """
    .. http:put:: /downloads

    A PUT request to this endpoint will start a download from a provided URI. This URI can either represent a file
    location, a magnet link or a HTTP(S) url.
    - anon_hops: the number of hops for the anonymous download. 0 hops is equivalent to a plain download
    - safe_seeding: whether the seeding of the download should be anonymous or not (0 = off, 1 = on)
    - destination: the download destination path of the torrent
    - torrent: the URI of the torrent file that should be downloaded. This parameter is required.

        **Example request**:

            .. sourcecode:: none

                curl -X PUT http://localhost:8085/downloads
                --data "anon_hops=2&safe_seeding=1&destination=/my/dest/on/disk/&uri=file:/home/me/test.torrent

        **Example response**:

            .. sourcecode:: javascript

                {"started": True, "infohash": "4344503b7e797ebf31582327a5baae35b11bda01"}
    """
    parameters = http.parse_qs(request.content.read(), 1)

    if "uri" not in parameters or len(parameters["uri"]) == 0:
        request.setResponseCode(http.BAD_REQUEST)
        return json.dumps({"error": "uri parameter missing"})

    download_config, error = DownloadsEndpoint.create_dconfig_from_params(parameters)
    if error:
        request.setResponseCode(http.BAD_REQUEST)
        return json.dumps({"error": error})

    def download_added(download):
        request.write(
            json.dumps(
                {
                    "started": True,
                    "infohash": download.get_def().get_infohash().encode("hex"),
                }
            )
        )
        request.finish()

    def on_error(error):
        request.setResponseCode(http.INTERNAL_SERVER_ERROR)
        request.write(json.dumps({"error": error.getErrorMessage()}))
        request.finish()

    download_deferred = self.session.start_download_from_uri(
        unquote_plus(unicode(parameters["uri"][0], "utf-8")), download_config
    )
    download_deferred.addCallback(download_added)
    download_deferred.addErrback(on_error)

    return NOT_DONE_YET


# Traceback (TypeError)
Traceback (most recent call last):
File "/home/quinten/Documents/tribler/TriblerGUI/tribler_window.py", line 633, in on_confirm_add_directory_dialog
self.tribler_settings['download_defaults']['saveas'], [], 0)
File "/home/quinten/Documents/tribler/TriblerGUI/tribler_window.py", line 369, in perform_start_download_request
if not is_dir_writable(destination):
File "/home/quinten/Documents/tribler/TriblerGUI/utilities.py", line 216, in is_dir_writable
if not os.path.exists(path):
File "/usr/lib/python2.7/genericpath.py", line 26, in exists
os.stat(path)
TypeError: coercing to Unicode: need string or buffer, NoneType found

# Fixed code
def render_PUT(self, request):
    """
    .. http:put:: /downloads

    A PUT request to this endpoint will start a download from a provided URI. This URI can either represent a file
    location, a magnet link or a HTTP(S) url.
    - anon_hops: the number of hops for the anonymous download. 0 hops is equivalent to a plain download
    - safe_seeding: whether the seeding of the download should be anonymous or not (0 = off, 1 = on)
    - destination: the download destination path of the torrent
    - torrent: the URI of the torrent file that should be downloaded. This parameter is required.

        **Example request**:

            .. sourcecode:: none

                curl -X PUT http://localhost:8085/downloads
                --data "anon_hops=2&safe_seeding=1&destination=/my/dest/on/disk/&uri=file:/home/me/test.torrent

        **Example response**:

            .. sourcecode:: javascript

                {"started": True, "infohash": "4344503b7e797ebf31582327a5baae35b11bda01"}
    """
    parameters = http.parse_qs(request.content.read(), 1)

    if "uri" not in parameters or len(parameters["uri"]) == 0:
        request.setResponseCode(http.BAD_REQUEST)
        return json.dumps({"error": "uri parameter missing"})

    download_config, error = DownloadsEndpoint.create_dconfig_from_params(parameters)
    if error:
        request.setResponseCode(http.BAD_REQUEST)
        return json.dumps({"error": error})

    def download_added(download):
        request.write(
            json.dumps(
                {
                    "started": True,
                    "infohash": download.get_def().get_infohash().encode("hex"),
                }
            )
        )
        request.finish()

    def on_error(error):
        request.setResponseCode(http.INTERNAL_SERVER_ERROR)
        request.write(json.dumps({"error": error.getErrorMessage()}))
        request.finish()

    uri = parameters["uri"][0]
    if uri.startswith("file:"):
        download_uri = "file:%s" % url2pathname(unicode(uri[5:], "utf-8"))
    else:
        download_uri = unquote_plus(unicode(uri, "utf-8"))
    download_deferred = self.session.start_download_from_uri(
        download_uri, download_config
    )
    download_deferred.addCallback(download_added)
    download_deferred.addErrback(on_error)

    return NOT_DONE_YET