def reply2har(reply, include_content=False, binary_content=False):
    """ Serialize QNetworkReply to HAR. """
    res = {
        "httpVersion": "HTTP/1.1",  # XXX: how to get HTTP version?
        "cookies": reply_cookies2har(reply),
        "headers": headers2har(reply),
        "content": {
            "size": 0,
            "mimeType": "",
        },
        "headersSize": headers_size(reply),
        # non-standard but useful
        "ok": not reply.error(),
        # non-standard, useful because reply url may not equal request url
        # in case of redirect
        "url": reply.url().toString()
    }

    content_type = reply.header(QNetworkRequest.ContentTypeHeader)
    if content_type is not None:
        res["content"]["mimeType"] = six.text_type(content_type)

    content_length = reply.header(QNetworkRequest.ContentLengthHeader)
    if content_length is not None:
        # this is not a correct way to get the size!
        res["content"]["size"] = content_length

    status = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
    if status is not None:
        res["status"] = int(status)
    else:
        res["status"] = 0

    status_text = reply.attribute(QNetworkRequest.HttpReasonPhraseAttribute)
    if status_text is not None:
        try:
            res["statusText"] = bytes(status_text, 'latin1').decode('latin1')
        except __HOLE__:
            res["statusText"] = bytes(status_text).decode('latin1')

    else:
        res["statusText"] = REQUEST_ERRORS_SHORT.get(reply.error(), "?")

    redirect_url = reply.attribute(QNetworkRequest.RedirectionTargetAttribute)
    if redirect_url is not None:
        res["redirectURL"] = six.text_type(redirect_url.toString())
    else:
        res["redirectURL"] = ""

    if include_content:
        data = bytes(reply.readAll())
        if binary_content:
            res["content"]["encoding"] = "binary"
            res["content"]["text"] = data
            res["content"]["size"] = len(data)
        else:
            res["content"]["encoding"] = "base64"
            res["content"]["text"] = base64.b64encode(data)
            res["content"]["size"] = len(data)

    return res
dataset/ETHPy150Open scrapinghub/splash/splash/har/qt.py/reply2har