Skip to content

Commit 0d15723

Browse files
Add HTTPClient, ported from the ESP8266 (#784)
Remove the need to have a separate WiFiClient that's destroyed after the HTTPClient. Let the object handle its own client, and pass through any SSL requests. Also supports the original ::begin methods which need a WiFiClient(Secure) to be passed in and managed by the app.
1 parent 6e0d6a2 commit 0d15723

File tree

29 files changed

+3126
-5
lines changed

29 files changed

+3126
-5
lines changed

docs/httpclient.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
HTTPClient Library
2+
==================
3+
4+
A simple HTTP requestor that can handle both HTTP and HTTP requests is
5+
included as the ``HTTPClient`` library.
6+
7+
Check the examples for use under HTTP and HTTPS configurations. In general,
8+
for HTTP connections (unsecured and very uncommon on the internet today) simply
9+
passing in a URL and performiung a GET is sufficient to transfer data.
10+
11+
.. code:: cpp
12+
13+
// Error checking is left as an exercise for the reader...
14+
HTTPClient http;
15+
if (http.begin("http://my.server/url")) {
16+
if (http.GET() > 0) {
17+
String data = http.getString();
18+
}
19+
http.end();
20+
}
21+
22+
For HTTPS connections, simply add the appropriate WiFiClientSecure calls
23+
as needed (i.e. ``setInsecure()``, ``setTrustAnchor``, etc.). See the
24+
WiFiClientSecure documentation for more details.
25+
26+
.. code:: cpp
27+
28+
// Error checking is left as an exercise for the reader...
29+
HTTPClient https;
30+
https.setInsecure(); // Use certs, but do not check their authenticity
31+
if (https.begin("https://my.secure.server/url")) {
32+
if (http.GET() > 0) {
33+
String data = http.getString();
34+
}
35+
http.end();
36+
}
37+
38+
Unlike the ESP8266 and ESP32 ``HTTPClient`` implementations it is not necessary
39+
to create a ``WiFiClient`` or ``WiFiClientSecure`` to pass in to the ``HTTPClient``
40+
object.

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ For the latest version, always check https://github.com/earlephilhower/arduino-p
5050
WiFiClientSecure (TLS/SSL/HTTPS) <bearssl-client-secure-class>
5151
WiFiServerSecure (TLS/SSL/HTTPS) <bearssl-server-secure-class>
5252

53+
HTTP/HTTPS Client <httpclient>
54+
5355
Over-the-Air (OTA) Updates <ota>
5456

5557
Ported/Optimized Libraries <libraries>

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# Syntax Coloring Map
33
#######################################
44

5+
Arduino KEYWORD3 RESERVED_WORD
6+
57
#######################################
68
# Datatypes (KEYWORD1)
79
#######################################
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
Authorization.ino
3+
4+
Created on: 09.12.2015
5+
6+
*/
7+
8+
#include <Arduino.h>
9+
#include <WiFi.h>
10+
#include <HTTPClient.h>
11+
12+
#ifndef STASSID
13+
#define STASSID "your-ssid"
14+
#define STAPSK "your-password"
15+
#endif
16+
17+
const char *ssid = STASSID;
18+
const char *pass = STAPSK;
19+
20+
WiFiMulti WiFiMulti;
21+
22+
void setup() {
23+
24+
Serial.begin(115200);
25+
// Serial.setDebugOutput(true);
26+
27+
Serial.println();
28+
Serial.println();
29+
Serial.println();
30+
31+
for (uint8_t t = 4; t > 0; t--) {
32+
Serial.printf("[SETUP] WAIT %d...\n", t);
33+
Serial.flush();
34+
delay(1000);
35+
}
36+
37+
WiFi.mode(WIFI_STA);
38+
WiFiMulti.addAP(ssid, pass);
39+
}
40+
41+
void loop() {
42+
// wait for WiFi connection
43+
if ((WiFiMulti.run() == WL_CONNECTED)) {
44+
45+
HTTPClient http;
46+
http.setInsecure();
47+
48+
Serial.print("[HTTP] begin...\n");
49+
// configure traged server and url
50+
51+
52+
http.begin("https://guest:guest@jigsaw.w3.org/HTTP/Basic/");
53+
54+
/*
55+
// or
56+
http.begin(client, "http://jigsaw.w3.org/HTTP/Basic/");
57+
http.setAuthorization("guest", "guest");
58+
59+
// or
60+
http.begin(client, "http://jigsaw.w3.org/HTTP/Basic/");
61+
http.setAuthorization("Z3Vlc3Q6Z3Vlc3Q=");
62+
*/
63+
64+
65+
Serial.print("[HTTP] GET...\n");
66+
// start connection and send HTTP header
67+
int httpCode = http.GET();
68+
69+
// httpCode will be negative on error
70+
if (httpCode > 0) {
71+
// HTTP header has been send and Server response header has been handled
72+
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
73+
74+
// file found at server
75+
if (httpCode == HTTP_CODE_OK) {
76+
String payload = http.getString();
77+
Serial.println(payload);
78+
}
79+
} else {
80+
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
81+
}
82+
83+
http.end();
84+
}
85+
86+
delay(10000);
87+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
BasicHTTPClient.ino
3+
4+
Created on: 24.05.2015
5+
6+
*/
7+
8+
#include <Arduino.h>
9+
#include <WiFi.h>
10+
#include <HTTPClient.h>
11+
12+
#ifndef STASSID
13+
#define STASSID "your-ssid"
14+
#define STAPSK "your-password"
15+
#endif
16+
17+
const char *ssid = STASSID;
18+
const char *pass = STAPSK;
19+
20+
WiFiMulti WiFiMulti;
21+
22+
void setup() {
23+
24+
Serial.begin(115200);
25+
// Serial.setDebugOutput(true);
26+
27+
Serial.println();
28+
Serial.println();
29+
Serial.println();
30+
31+
for (uint8_t t = 4; t > 0; t--) {
32+
Serial.printf("[SETUP] WAIT %d...\n", t);
33+
Serial.flush();
34+
delay(1000);
35+
}
36+
37+
WiFiMulti.addAP(ssid, pass);
38+
}
39+
40+
void loop() {
41+
// wait for WiFi connection
42+
if ((WiFiMulti.run() == WL_CONNECTED)) {
43+
44+
HTTPClient http;
45+
46+
Serial.print("[HTTP] begin...\n");
47+
if (http.begin("http://httpbin.org")) { // HTTP
48+
49+
50+
Serial.print("[HTTP] GET...\n");
51+
// start connection and send HTTP header
52+
int httpCode = http.GET();
53+
54+
// httpCode will be negative on error
55+
if (httpCode > 0) {
56+
// HTTP header has been send and Server response header has been handled
57+
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
58+
59+
// file found at server
60+
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
61+
String payload = http.getString();
62+
Serial.println(payload);
63+
}
64+
} else {
65+
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
66+
}
67+
68+
http.end();
69+
} else {
70+
Serial.printf("[HTTP} Unable to connect\n");
71+
}
72+
}
73+
74+
delay(10000);
75+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
BasicHTTPSClient-Hard.ino
3+
4+
Demonstrates the manual way of making a WiFiClient and passing it in to the HTTPClient
5+
6+
Created on: 20.08.2018
7+
8+
*/
9+
10+
#include <Arduino.h>
11+
#include <WiFi.h>
12+
#include <HTTPClient.h>
13+
14+
#ifndef STASSID
15+
#define STASSID "your-ssid"
16+
#define STAPSK "your-password"
17+
#endif
18+
19+
const char *ssid = STASSID;
20+
const char *pass = STAPSK;
21+
22+
WiFiMulti WiFiMulti;
23+
24+
void setup() {
25+
26+
Serial.begin(115200);
27+
28+
Serial.println();
29+
Serial.println();
30+
Serial.println();
31+
32+
for (uint8_t t = 4; t > 0; t--) {
33+
Serial.printf("[SETUP] WAIT %d...\n", t);
34+
Serial.flush();
35+
delay(1000);
36+
}
37+
38+
WiFi.mode(WIFI_STA);
39+
WiFiMulti.addAP(ssid, pass);
40+
}
41+
42+
void loop() {
43+
// wait for WiFi connection
44+
if ((WiFiMulti.run() == WL_CONNECTED)) {
45+
46+
WiFiClientSecure client;
47+
client.setInsecure(); // Not safe against MITM attacks
48+
49+
HTTPClient https;
50+
51+
Serial.print("[HTTPS] begin...\n");
52+
if (https.begin(client, "https://jigsaw.w3.org/HTTP/connection.html")) { // HTTPS
53+
54+
Serial.print("[HTTPS] GET...\n");
55+
// start connection and send HTTP header
56+
int httpCode = https.GET();
57+
58+
// httpCode will be negative on error
59+
if (httpCode > 0) {
60+
// HTTP header has been send and Server response header has been handled
61+
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
62+
63+
// file found at server
64+
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
65+
String payload = https.getString();
66+
Serial.println(payload);
67+
}
68+
} else {
69+
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
70+
}
71+
72+
https.end();
73+
} else {
74+
Serial.printf("[HTTPS] Unable to connect\n");
75+
}
76+
}
77+
78+
Serial.println("Wait 10s before next round...");
79+
delay(10000);
80+
}

0 commit comments

Comments
 (0)