{"id":575,"date":"2017-12-13T19:15:28","date_gmt":"2017-12-13T11:15:28","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=575"},"modified":"2018-11-17T22:05:49","modified_gmt":"2018-11-17T14:05:49","slug":"how-to-construct-a-http-request-to-an-endpoint-with-http-basic-authentication","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-construct-a-http-request-to-an-endpoint-with-http-basic-authentication\/","title":{"rendered":"How to construct a HTTP request to an endpoint with HTTP Basic Authentication"},"content":{"rendered":"<p>The HTTP basic authentication is a mechanism commonly used by web servers to authenticate the sender of a HTTP request. Such a mechanism is usually used to guard server endpoints that are meant to be accessed programmatically.<\/p>\n<p>Almost every new project that I got my hands on required me to create client side coding for accessing server endpoints that use HTTP basic authentication for authenticating the HTTP request sender.<\/p>\n<p>To have a quick reference on how to construct a HTTP request to an endpoint with HTTP basic authentication, I created this post to as a documentation. The steps are agnostic to any programming languages.<\/p>\n<h2>Generating the HTTP Basic Authentication string payload from a username and password pair<\/h2>\n<p>The first step to constructing the HTTP request to an endpoint with HTTP basic authentication is to generate a string payload from a username and password pair.<\/p>\n<p>Assuming that we have the username and password in the variables <code>username<\/code> and <code>password<\/code>, we typically go through the following steps to produce the string <code>payload<\/code>: <\/p>\n<ol>\n<li>Concatenate username and password with a colon in between:\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nucolonp = username + ':' + password\r\n<\/pre>\n<\/li>\n<li>Convert <code>ucolonp<\/code> to base 64 encoding format and prepend the result with the string 'Basic ':\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\npayload = 'Basic ' + base64encode(ucolonp)\r\n<\/pre>\n<\/li>\n<\/ol>\n<p>Alternatively, we can collapse the above steps into the following code execution:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\npayload = 'Basic ' + base64encode(username + ':' + password)\r\n<\/pre>\n<p>Most programming languages included some library functions to help us with encoding a string in Base64 format. You should replace <code>base64encode<\/code> with the respective library function call in the programming language of your choice.<\/p>\n<h2>Appending the HTTP Basic Authentication string payload as a HTTP header in the HTTP request<\/h2>\n<p>Once you have the Base64 encoded string payload, the last step will be to include it as a HTTP header in the HTTP request that you are going to send to the server. The HTTP header starts with the string \"Authorization: \" followed by the payload that we had generated earlier.<\/p>\n<p>Typically, the HTTP header is included as a key value pair to a function call, with the string \"Authorization\" as the key and the base 64 encoded string payload as the value. In such a case, the colon is usually omitted:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nhttpRequest.addHeader('Authorization', payload)\r\n<\/pre>\n<p>You should replace the <code>httpRequest.addHeader<\/code> with the respective library function call in the programming language of your choice.<\/p>\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-9h\" 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-9h&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-9h&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%2F575&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>The HTTP basic authentication is a mechanism commonly used by web servers to authenticate the sender of a HTTP request. Such a mechanism is usually used to guard server endpoints that are meant to be accessed programmatically.<\/p>\n<p>Almost every new project that I got my hands on required me to create client side coding for accessing server endpoints that use HTTP basic authentication for authenticating the HTTP request sender.<\/p>\n<p>To have a quick reference on how to construct a HTTP request to an endpoint with HTTP basic authentication, I created this post to as a documentation. The steps are agnostic to any programming languages.<\/p>\n","protected":false},"author":1,"featured_media":1220,"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,4],"tags":[23,403],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Java-logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-9h","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/575"}],"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=575"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/575\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1220"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}