{"id":1373,"date":"2018-11-17T22:45:56","date_gmt":"2018-11-17T14:45:56","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1373"},"modified":"2018-11-17T22:48:43","modified_gmt":"2018-11-17T14:48:43","slug":"how-to-send-an-http-request-to-a-http-basic-authentication-endpoint-in-python-3-with-requests-library","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-send-an-http-request-to-a-http-basic-authentication-endpoint-in-python-3-with-requests-library\/","title":{"rendered":"How to send an HTTP request to a HTTP Basic Authentication endpoint in Python 3 with requests library"},"content":{"rendered":"<p>When you are building a Python 3 application for the Internet, you could encounter API endpoints that use HTTP Basic Authentication as the authentication mechanism.<\/p>\n<p>In such a situation, using the <a href=\"http:\/\/docs.python-requests.org\/en\/master\/\" rel=\"noopener\" target=\"_blank\">requests library<\/a> in your Python 3 code makes it easier to communicate with those endpoints.<\/p>\n<p>In case you need to build a Python 3 application that sends <a href=\"https:\/\/www.techcoil.com\/glossary\/http-request\/\" target=\"_blank\" rel=\"noopener\">HTTP request<\/a> to a HTTP Basic Authentication endpoint, this is how you can do so with the requests library.<\/p>\n<h2>How to construct a HTTP request to an endpoint with HTTP Basic Authentication in Python 3<\/h2>\n<p>When you want to <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-construct-a-http-request-to-an-endpoint-with-http-basic-authentication\/\" rel=\"noopener\" target=\"_blank\">construct a HTTP request to an endpoint with HTTP Basic Authentication<\/a> from scratch, there are several procedures to follow. Therefore, the creators of the requests library had made it easy for us to construct a HTTP request to an endpoint with HTTP Basic Authentication easily.<\/p>\n<p>With the requests library, we can either encapsulate the username and password pair in an instance of <a href=\"http:\/\/docs.python-requests.org\/en\/master\/api\/#requests.auth.HTTPBasicAuth\" rel=\"noopener\" target=\"_blank\">requests.auth.HTTPBasicAuth<\/a> or in a tuple:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nbasicAuthCredentials = HTTPBasicAuth('username', 'password')\r\n# Or\r\nbasicAuthCredentials = ('username', 'password')\r\n<\/pre>\n<p>After we had done so, we then pass in <code>basicAuthCredentials<\/code> as the value to the <code>auth<\/code> parameter inside one of the following functions:<\/p>\n<ul>\n<li><a href=\"http:\/\/docs.python-requests.org\/en\/master\/api\/#requests.request\" rel=\"noopener\" target=\"_blank\"><code>requests.request<\/code><\/a><\/li>\n<li><a href=\"http:\/\/docs.python-requests.org\/en\/master\/api\/#requests.head\" rel=\"noopener\" target=\"_blank\"><code>requests.head<\/code><\/a><\/li>\n<li><a href=\"http:\/\/docs.python-requests.org\/en\/master\/api\/#requests.get\" rel=\"noopener\" target=\"_blank\"><code>requests.get<\/code><\/a><\/li>\n<li><a href=\"http:\/\/docs.python-requests.org\/en\/master\/api\/#requests.post\" rel=\"noopener\" target=\"_blank\"><code>requests.post<\/code><\/a><\/li>\n<li><a href=\"http:\/\/docs.python-requests.org\/en\/master\/api\/#requests.put\" rel=\"noopener\" target=\"_blank\"><code>requests.put<\/code><\/a><\/li>\n<li><a href=\"http:\/\/docs.python-requests.org\/en\/master\/api\/#requests.patch\" rel=\"noopener\" target=\"_blank\"><code>requests.patch<\/code><\/a><\/li>\n<li><a href=\"http:\/\/docs.python-requests.org\/en\/master\/api\/#requests.delete\" rel=\"noopener\" target=\"_blank\"><code>requests.delete<\/code><\/a><\/li>\n<\/ul>\n<h2>Python 3 example for sending a HTTP GET request to a HTTP Basic Authentication endpoint with the request library<\/h2>\n<p>For example, if we want to send a HTTP GET request to a HTTP Basic Authentication endpoint, we can write Python 3 codes similar to the following:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport requests\r\n#\r\nbasicAuthCredentials = ('username', 'password')\r\n\r\n# Send HTTP GET request to server and attempt to receive a response\r\nresponse = requests.get('http:\/\/httpbin.org\/basic-auth\/username\/password', auth=basicAuthCredentials)\r\n     \r\n# If the HTTP GET request can be served\r\nif response.status_code == 200:\r\n    print(response.text)\r\n<\/pre>\n<p>After the above set of codes is run, you should see the following output in your terminal:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n{\r\n  &quot;authenticated&quot;: true, \r\n  &quot;user&quot;: &quot;user&quot;\r\n}\r\n<\/pre>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-m9\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-m9&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-m9&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1373&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>When you are building a Python 3 application for the Internet, you could encounter API endpoints that use HTTP Basic Authentication as the authentication mechanism.<\/p>\n<p>In such a situation, using the <a href=\"http:\/\/docs.python-requests.org\/en\/master\/\" rel=\"noopener\" target=\"_blank\">requests library<\/a> in your Python 3 code makes it easier to communicate with those endpoints.<\/p>\n<p>In case you need to build a Python 3 application that sends <a href=\"https:\/\/www.techcoil.com\/glossary\/http-request\/\" target=\"_blank\" rel=\"noopener\">HTTP request<\/a> to a HTTP Basic Authentication endpoint, this is how you can do so with the requests library.<\/p>\n","protected":false},"author":1,"featured_media":1244,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[23,403,59,226,233,551],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Python-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-m9","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1373"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=1373"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1373\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1244"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}