# Buggy code
def get_url_hook(response, **kwargs):
    """Get URL hook."""
    log.debug(
        "{0} URL: {1} [Status: {2}]",
        response.request.method,
        response.request.url,
        response.status_code,
    )

    if response.request.method == "POST":
        data = response.request.body.decode("utf-8")
        log.debug("With post data: {0}", data)


# Traceback (UnicodeDecodeError)
2017-09-10 12:17:25 DEBUG    Thread-16 :: [3f23fd2] Broken providers already updated in the last hour
2017-09-10 12:17:33 DEBUG    Thread-16 :: [3f23fd2] Broken providers already updated in the last hour
2017-09-10 12:17:46 DEBUG    POSTPROCESSOR :: [3f23fd2] Starting new thread: POSTPROCESSOR
2017-09-10 12:17:47 DEBUG    POSTPROCESSOR :: [3f23fd2] Processing path: /volume1/downloads/downloads_tv
2017-09-10 12:17:47 DEBUG    POSTPROCESSOR :: [3f23fd2] No processable items found in folder: /volume1/downloads/downloads_tv
2017-09-10 12:17:47 INFO     POSTPROCESSOR :: [3f23fd2] Successfully processed.
2017-09-10 12:18:09 INFO     SNATCHQUEUE-MANUALSNATCH-73141 :: [3f23fd2] Beginning to manual snatch release: American.Dad.S13E21.720p.HDTV.x264-AVS
2017-09-10 12:18:09 INFO     SNATCHQUEUE-MANUALSNATCH-73141 :: [3f23fd2] Downloading American.Dad.S13E21.720p.HDTV.x264-AVS with 145 seeders and 1 leechers and size 303.00 MB from IPTorrents
2017-09-10 12:18:09 DEBUG    SNATCHQUEUE-MANUALSNATCH-73141 :: [3f23fd2] GET URL: https://iptorrents.eu/download.php/2455049/American.Dad.S13E21.720p.HDTV.x264-AVS.torrent [Status: 200]
2017-09-10 12:18:09 DEBUG    SNATCHQUEUE-MANUALSNATCH-73141 :: [3f23fd2] User-Agent: Mozilla/5.0 (Windows NT 4.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36
2017-09-10 12:18:09 DEBUG    SNATCHQUEUE-MANUALSNATCH-73141 :: [3f23fd2] Calling DownloadStation Client
2017-09-10 12:18:10 DEBUG    SNATCHQUEUE-MANUALSNATCH-73141 :: [3f23fd2] GET URL: http://localhost:9092/webapi/auth.cgi?session=DownloadStation&account=**********&version=2&format=cookie&passwd=**********&api=SYNO.API.Auth&method=login [Status: 200]
2017-09-10 12:18:10 DEBUG    SNATCHQUEUE-MANUALSNATCH-73141 :: [3f23fd2] User-Agent: Medusa/0.0.1(Linux; 3.2.40; d5133f11-960f-11e7-8c5b-001132374582)
2017-09-10 12:18:10 DEBUG    SNATCHQUEUE-MANUALSNATCH-73141 :: [3f23fd2] GET URL: http://localhost:9092/webapi/entry.cgi?session=DownloadStation&api=SYNO.DSM.Info&version=2&method=getinfo [Status: 200]
2017-09-10 12:18:10 DEBUG    SNATCHQUEUE-MANUALSNATCH-73141 :: [3f23fd2] User-Agent: Medusa/0.0.1(Linux; 3.2.40; d5133f11-960f-11e7-8c5b-001132374582)
2017-09-10 12:18:10 INFO     SNATCHQUEUE-MANUALSNATCH-73141 :: [3f23fd2] Destination is now downloads/downloads_tv
2017-09-10 12:18:10 DEBUG    SNATCHQUEUE-MANUALSNATCH-73141 :: [3f23fd2] DownloadStation: Requested a POST connection to http://localhost:9092/webapi/DownloadStation/task.cgi with params: None Data: {u'session': u'DownloadStation', u'destination': u'/volume1/downloads/downloads_tv', u'api': u'SYNO...
2017-09-10 12:18:10 DEBUG    SNATCHQUEUE-MANUALSNATCH-73141 :: [3f23fd2] POST URL: http://localhost:9092/webapi/DownloadStation/task.cgi [Status: 200]
2017-09-10 12:18:10 DEBUG    SNATCHQUEUE-MANUALSNATCH-73141 :: [3f23fd2] User-Agent: Medusa/0.0.1(Linux; 3.2.40; d5133f11-960f-11e7-8c5b-001132374582)
2017-09-10 12:18:10 ERROR    SNATCHQUEUE-MANUALSNATCH-73141 :: [3f23fd2] DownloadStation: Unknown exception raised when send torrent to DownloadStation : 'utf8' codec can't decode byte 0xc0 in position 1738: invalid start byte
Traceback (most recent call last):
  File "/volume1/@appstore/sickbeard-custom/var/SickBeard/medusa/clients/torrent/generic.py", line 79, in _request
    cookies=cookies, timeout=120, verify=False)
  File "/volume1/@appstore/sickbeard-custom/var/SickBeard/lib/requests/sessions.py", line 518, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/volume1/@appstore/sickbeard-custom/var/SickBeard/lib/requests/sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "/volume1/@appstore/sickbeard-custom/var/SickBeard/lib/requests/sessions.py", line 591, in send
    r = dispatch_hook('response', hooks, r, **kwargs)
  File "/volume1/@appstore/sickbeard-custom/var/SickBeard/lib/requests/hooks.py", line 31, in dispatch_hook
    _hook_data = hook(hook_data, **kwargs)
  File "/volume1/@appstore/sickbeard-custom/var/SickBeard/medusa/session/hooks.py", line 28, in log_url
    log.debug('With post data: {data}'.format(data=resp.request.body))
  File "/usr/local/sickbeard-custom/env/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xc0 in position 1738: invalid start byte

# Fixed code
def get_url_hook(response, **kwargs):
    """Get URL hook."""
    request = response.request
    log.debug(
        "{0} URL: {1} [Status: {2}]", request.method, request.url, response.status_code
    )

    if request.method == "POST":
        body = request.body
        # try to log post data using various codecs to decode
        codecs = ("utf-8", "latin1", "cp1252")
        for codec in codecs:
            try:
                data = body.decode(codec)
            except UnicodeError as error:
                log.debug(
                    "Failed to decode post data as {codec}: {msg}",
                    codec=codec,
                    msg=error,
                )
            else:
                log.debug("With post data: {0}", data)
                break
        else:
            log.warning("Failed to decode post data with {codecs}", codecs=codecs)