[{"data":1,"prerenderedAt":73},["Reactive",2],{"/samples/python/httplib2":3,"related:QDdZxmDWzO":59},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"language":10,"library":8,"link":11,"body":12,"_type":54,"_id":55,"_source":56,"_file":57,"_extension":58},"/samples/python/httplib2","python",false,"","Httplib2","Httplib2 is a comprehensive HTTP client library for Python. It provides support for features like caching, authentication, SSL/TLS, and proxy handling. httplib2 offers a higher-level API compared to Python's built-in http.client module and is commonly used for making HTTP requests and handling responses in Python applications.","Python","https://httplib2.readthedocs.io/en/latest/",{"type":13,"children":14,"toc":51},"root",[15,30,42],{"type":16,"tag":17,"props":18,"children":19},"element","p",{},[20,28],{"type":16,"tag":21,"props":22,"children":25},"a",{"href":11,"rel":23},[24],"nofollow",[26],{"type":27,"value":8},"text",{"type":27,"value":29}," is a comprehensive HTTP client library for Python. It provides support for features like caching, authentication, SSL/TLS, and proxy handling. httplib2 offers a higher-level API compared to Python's built-in http.client module and is commonly used for making HTTP requests and handling responses in Python applications.",{"type":16,"tag":31,"props":32,"children":36},"pre",{"className":33,"code":35,"language":5,"meta":7},[34],"language-python","import httplib2\nimport json\n\ndef convert(api_key, params, endpoint='pdf'):\n    \"\"\"\n    We focused on making this function as simple as possible.\n    Since PDFShift is a REST API, we just need to send a\n    POST request to the endpoint by passing a set of custom parameters.\n    \n    Args:\n        api_key (str): Your API key.\n        params (dict): A dictionary containing the parameters\n                       you want to send to the API.\n        endpoint (str): The type of conversion you want to perform\n                        (pdf, png, jpg, webp)\n    \n    Returns:\n        bytes | dict: Either the binary content of the PDF\n                      or a dictionary containing the details\n                      when filename/webhook are passed\n    \"\"\"\n    \n    assert endpoint in ('pdf', 'png', 'jpg', 'webp')\n    \n    # Set the API URL\n    url = f'https://api.pdfshift.io/v3/convert/{endpoint}'\n    \n    # Create an httplib2.Http instance\n    http = httplib2.Http()\n\n    # Set headers\n    headers = {\n        'Content-Type': 'application/json',\n        'X-API-Key': api_key\n    }\n    \n    # Convert params to JSON\n    body = json.dumps(params)\n    \n    # Make the POST request\n    response, content = http.request(url, method='POST', body=body, headers=headers)\n    \n    # Check for successful response\n    if response.status >= 400:\n        raise ValueError(f\"Request failed with status code {response.status}: {content.decode('utf-8')}\")\n    \n    # Decode the content\n    decoded_content = content.decode('utf-8')\n    \n    # Return the response based on the presence of filename or webhook\n    if 'filename' in params or 'webhook' in params:\n        return json.loads(decoded_content)\n    \n    return decoded_content\n",[37],{"type":16,"tag":38,"props":39,"children":40},"code",{"__ignoreMap":7},[41],{"type":27,"value":35},{"type":16,"tag":31,"props":43,"children":46},{"className":44,"code":45,"language":5,"meta":7},[34],"binary = convert('sk_XXXXXXXXXXXXXX', {'source': 'https://en.wikipedia.org/wiki/REST'})\nwith open('result.pdf', 'wb') as f:\n    f.write(binary)\n",[47],{"type":16,"tag":38,"props":48,"children":49},{"__ignoreMap":7},[50],{"type":27,"value":45},{"title":7,"searchDepth":52,"depth":52,"links":53},2,[],"markdown","content:samples:python:httplib2.md","content","samples/python/httplib2.md","md",[60,64,65,69],{"_path":61,"language":10,"library":62,"_id":63},"/samples/python/aiohttp","Aiohttp","content:samples:python:aiohttp.md",{"_path":4,"language":10,"library":8,"_id":55},{"_path":66,"language":10,"library":67,"_id":68},"/samples/python/requests","Requests","content:samples:python:requests.md",{"_path":70,"language":10,"library":71,"_id":72},"/samples/python/urllib3","Urllib3","content:samples:python:urllib3.md",1785426823450]