{"id":1929,"date":"2020-01-04T00:23:10","date_gmt":"2020-01-03T16:23:10","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1929"},"modified":"2020-05-12T14:07:31","modified_gmt":"2020-05-12T06:07:31","slug":"how-to-send-a-http-basic-authentication-request-from-your-esp32-development-board","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-send-a-http-basic-authentication-request-from-your-esp32-development-board\/","title":{"rendered":"How to send a HTTP Basic Authentication request from your ESP32 development board"},"content":{"rendered":"<p>When you connect your ESP32 board to the internet, you may need to send HTTP Basic Authentication requests.<\/p>\n<p>In this situation, we can rely on the <a href=\"https:\/\/github.com\/espressif\/arduino-esp32\/tree\/master\/libraries\/HTTPClient\" rel=\"noopener\" target=\"_blank\">HttpClient library<\/a> from the <a href=\"https:\/\/github.com\/espressif\/arduino-esp32\" rel=\"noopener\" target=\"_blank\">Arduino core for ESP32<\/a>.<\/p>\n<p>Given that, this post shows how you can send a <a href=\"https:\/\/www.techcoil.com\/glossary\/http\/\" rel=\"noopener\" target=\"_blank\">HTTP<\/a> Basic Authentication request from your ESP32 development board.<\/p>\n<h2>Enabling ESP32 Development on Arduino IDE<\/h2>\n<p>Before continuing, be sure that you have <a href=\"https:\/\/www.techcoil.com\/blog\/enabling-esp32-development-on-arduino-ide\/\" rel=\"noopener\" target=\"_blank\">enable ESP32 Development on Arduino IDE<\/a>. <\/p>\n<p>After you have done so, you should be able to use the HttpClient library from your Arduino sketch.<\/p>\n<h2>Writing the Arduino Sketch to send a HTTP Basic Authentication request from your ESP32 development board<\/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 steps to follow. However, the HttpClient library had simplified these steps for us.<\/p>\n<p>Given that, let's look at an example Arduino Sketch that sends HTTP Basic Authentication requests to a server endpoint:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;HTTPClient.h&gt;\r\n#include &lt;WiFiMulti.h&gt;\r\n\r\nconst char *AP_SSID = &quot;yourAPSSID&quot;;\r\nconst char *AP_PWD = &quot;yourAPPwd&quot;;\r\n\r\nWiFiMulti wifiMulti;\r\nvoid setup() {\r\n  Serial.begin(9600);\r\n\r\n  delay(4000);\r\n  wifiMulti.addAP(AP_SSID, AP_PWD);\r\n  \r\n}\r\n\r\nvoid loop() {\r\n\r\n  \/\/ Block until we are able to connect to the WiFi access point\r\n  if (wifiMulti.run() == WL_CONNECTED) {\r\n    HTTPClient httpClient;\r\n    \r\n    httpClient.begin(&quot;https:\/\/httpbin.org\/basic-auth\/username\/password&quot;);\r\n    httpClient.setAuthorization(&quot;username&quot;, &quot;password&quot;);\r\n\r\n    int statusCode = httpClient.GET();\r\n\r\n    if (statusCode &gt; 0) {\r\n\r\n      Serial.println(&quot;Able to send HTTP request out of the board.&quot;);\r\n      \r\n      if(statusCode == HTTP_CODE_OK) {\r\n        Serial.println(&quot;Server responded with HTTP status 200.&quot;);\r\n        String payload = httpClient.getString();\r\n        Serial.println(payload);\r\n      }\r\n      else {\r\n        Serial.printf(&quot;Got HTTP status: %d&quot;, statusCode);\r\n        String payload = httpClient.getString();\r\n        Serial.println(payload);\r\n      }\r\n    }\r\n    else {\r\n      Serial.printf(&quot;Error occurred while sending HTTP Get: %s\\n&quot;, httpClient.errorToString(statusCode).c_str());\r\n    }\r\n\r\n    \/\/ Release the resources used\r\n    httpClient.end();\r\n  }\r\n\r\n  delay(3000);\r\n}\r\n<\/pre>\n<p>So what is the above Arduino Sketch doing?<\/p>\n<p>First, it imports the <code>HTTPClient<\/code> and <a href=\"https:\/\/github.com\/espressif\/arduino-esp32\/blob\/master\/libraries\/WiFi\/src\/WiFiMulti.h\" rel=\"noopener\" target=\"_blank\"><code>WiFiMulti<\/code><\/a> libraries to help with connect to the internet.<\/p>\n<p>After that, we define the SSID and password of the wireless access point that our ESP32 board will connect with. If you are using a different set of wireless credentials, then <strong>be sure to change them<\/strong>. <\/p>\n<p>Next up, we create an instance of <code>WiFiMulti<\/code>.<\/p>\n<h3>Inside the <code>setup<\/code> function<\/h3>\n<p>When our ESP32 board runs our <code>setup<\/code> function, it defines a baud rate of 9600 for the board's serial connection. After that, it waits for 4 seconds before adding the wireless credentials to wifiMulti.<\/p>\n<h3>Inside the <code>loop<\/code> function<\/h3>\n<p>After setting up the WiFi connection, our ESP32 board will run the <code>loop<\/code> function.<\/p>\n<p>Whenever the loop function is ran, we call <code>wifiMulti.run()<\/code> to establish a connection to the wireless network. At this point in time, code execution will stall until our ESP32 board is able to connect to the wireless network.<\/p>\n<p>When the connection is established successfully, the code will attempt to send the HTTP Basic Authentication request.<\/p>\n<p>In order for that to happen, we first create an instance of <code>HTTPClient<\/code>. After that we include the full URL of the server endpoint with a call to <code>httpClient.begin<\/code>. In case you are wondering, the URL will reach a server endpoint for testing out basic authentication requests with (username, password) as the credentials.<\/p>\n<p>After that we call <code>httpClient.GET<\/code> to send the <a href=\"https:\/\/www.techcoil.com\/glossary\/http-request\/\" rel=\"noopener\" target=\"_blank\">HTTP request<\/a> with GET as the request method. When the call returns, we will get the status code of the <a href=\"https:\/\/www.techcoil.com\/glossary\/http-response\/\" rel=\"noopener\" target=\"_blank\">HTTP response<\/a> that the server returns back to us.<\/p>\n<p>At this point in time, we will know whether the HTTP request had been sent out successfully. When we get a positive number, we know that the server has returned us with a response.<\/p>\n<p>If <code>statusCode == HTTP_CODE_OK<\/code>, then we proceed to print the response body out to serial. However, if <code>statusCode<\/code> is not <code>HTTP_CODE_OK<\/code>, we print out the status code that we have received. When you change the credentials in the URL, you should be able to see this condition block being executed.<\/p>\n<p>In case <code>statusCode<\/code> is negative, we print the error message to serial. After the checks on <code>statusCode<\/code>, we call <code>httpClient.end<\/code> to release the resources being used.<\/p>\n<p>Finally, the last statement of the <code>loop<\/code> function delays the execution of the loop by 3 seconds.<\/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-v7\" 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-v7&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-v7&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%2F1929&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 connect your ESP32 board to the internet, you may need to send HTTP Basic Authentication requests.<\/p>\n<p>In this situation, we can rely on the <a href=\"https:\/\/github.com\/espressif\/arduino-esp32\/tree\/master\/libraries\/HTTPClient\" rel=\"noopener\" target=\"_blank\">HttpClient library<\/a> from the <a href=\"https:\/\/github.com\/espressif\/arduino-esp32\" rel=\"noopener\" target=\"_blank\">Arduino core for ESP32<\/a>.<\/p>\n<p>Given that, this post shows how you can send a <a href=\"https:\/\/www.techcoil.com\/glossary\/http\/\" rel=\"noopener\" target=\"_blank\">HTTP<\/a> Basic Authentication request from your ESP32 development board.<\/p>\n","protected":false},"author":1,"featured_media":1930,"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":[4],"tags":[588,630,403,714,484,715],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Espressif-ESP32-WROOM-32D-development-board-connected-to-USB.jpg","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-v7","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1929"}],"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=1929"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1929\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1930"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}