[{"data":1,"prerenderedAt":73},["Reactive",2],{"/samples/python/urllib3":3,"related:ie9L65440Y":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/urllib3","python",false,"","Urllib3","Urllib3 is a powerful HTTP client library for Python. It builds upon Python's built-in http.client module and provides features like connection pooling, SSL/TLS verification, and request retries. urllib3 offers a higher-level API compared to http.client and is commonly used for making secure and reliable HTTP requests in Python applications.","Python","https://urllib3.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 powerful HTTP client library for Python. It builds upon Python's built-in http.client module and provides features like connection pooling, SSL/TLS verification, and request retries. urllib3 offers a higher-level API compared to http.client and is commonly used for making secure and reliable HTTP requests in Python applications.",{"type":16,"tag":31,"props":32,"children":36},"pre",{"className":33,"code":35,"language":5,"meta":7},[34],"language-python","import json\nimport urllib3\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    http = urllib3.PoolManager()\n    \n    url = f'https://api.pdfshift.io/v3/convert/{endpoint}'\n    body = json.dumps(params).encode('utf-8')\n    headers = urllib3.util.make_headers()\n    headers['X-API-Key'] = api_key\n    headers['Content-Type'] = 'application/json'\n    response = http.request('POST', url, headers=headers, body=body)\n    \n    if response.status >= 400:\n        raise ValueError(f\"Request failed with status code {response.status}: {response.data.decode('utf-8')}\")\n    \n    if 'filename' in params or 'webhook' in params:\n        return json.loads(response.data.decode('utf-8'))\n    \n    return response.data\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:urllib3.md","content","samples/python/urllib3.md","md",[60,64,68,72],{"_path":61,"language":10,"library":62,"_id":63},"/samples/python/aiohttp","Aiohttp","content:samples:python:aiohttp.md",{"_path":65,"language":10,"library":66,"_id":67},"/samples/python/httplib2","Httplib2","content:samples:python:httplib2.md",{"_path":69,"language":10,"library":70,"_id":71},"/samples/python/requests","Requests","content:samples:python:requests.md",{"_path":4,"language":10,"library":8,"_id":55},1785426823460]