mirror of
https://github.com/arduino-libraries/ArduinoHttpClient.git
synced 2025-04-19 21:22:15 +03:00
Updated examples and readme.md
This commit is contained in:
parent
b6424e430d
commit
5e1f1f21bf
@ -7,6 +7,7 @@ Derived from [Adrian McEwen's HttpClient library](https://github.com/amcewen/Htt
|
|||||||
## Dependencies
|
## Dependencies
|
||||||
|
|
||||||
- Requires a networking hardware and a library that provides transport specific `Client` instance, such as:
|
- Requires a networking hardware and a library that provides transport specific `Client` instance, such as:
|
||||||
|
- [WiFiNINA](https://github.com/arduino-libraries/WiFiNINA)
|
||||||
- [WiFi101](https://github.com/arduino-libraries/WiFi101)
|
- [WiFi101](https://github.com/arduino-libraries/WiFi101)
|
||||||
- [Ethernet](https://github.com/arduino-libraries/Ethernet)
|
- [Ethernet](https://github.com/arduino-libraries/Ethernet)
|
||||||
- [WiFi](https://github.com/arduino-libraries/WiFi)
|
- [WiFi](https://github.com/arduino-libraries/WiFi)
|
||||||
|
@ -2,12 +2,12 @@
|
|||||||
GET client with HTTP basic authentication for ArduinoHttpClient library
|
GET client with HTTP basic authentication for ArduinoHttpClient library
|
||||||
Connects to server once every five seconds, sends a GET request
|
Connects to server once every five seconds, sends a GET request
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
created 14 Feb 2016
|
created 14 Feb 2016
|
||||||
by Tom Igoe
|
by Tom Igoe
|
||||||
modified 3 Jan 2017 to add HTTP basic authentication
|
modified 3 Jan 2017 to add HTTP basic authentication
|
||||||
by Sandeep Mistry
|
by Sandeep Mistry
|
||||||
|
modified 22 Jan 2019
|
||||||
|
by Tom Igoe
|
||||||
|
|
||||||
this example is in the public domain
|
this example is in the public domain
|
||||||
*/
|
*/
|
||||||
@ -25,8 +25,6 @@ int port = 8080;
|
|||||||
WiFiClient wifi;
|
WiFiClient wifi;
|
||||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||||
int status = WL_IDLE_STATUS;
|
int status = WL_IDLE_STATUS;
|
||||||
String response;
|
|
||||||
int statusCode = 0;
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
@ -56,8 +54,8 @@ void loop() {
|
|||||||
client.endRequest();
|
client.endRequest();
|
||||||
|
|
||||||
// read the status code and body of the response
|
// read the status code and body of the response
|
||||||
statusCode = client.responseStatusCode();
|
int statusCode = client.responseStatusCode();
|
||||||
response = client.responseBody();
|
String response = client.responseBody();
|
||||||
|
|
||||||
Serial.print("Status code: ");
|
Serial.print("Status code: ");
|
||||||
Serial.println(statusCode);
|
Serial.println(statusCode);
|
||||||
@ -66,4 +64,3 @@ void loop() {
|
|||||||
Serial.println("Wait five seconds");
|
Serial.println("Wait five seconds");
|
||||||
delay(5000);
|
delay(5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,14 +2,10 @@
|
|||||||
Custom request header example for the ArduinoHttpClient
|
Custom request header example for the ArduinoHttpClient
|
||||||
library. This example sends a GET and a POST request with a custom header every 5 seconds.
|
library. This example sends a GET and a POST request with a custom header every 5 seconds.
|
||||||
|
|
||||||
note: WiFi SSID and password are stored in config.h file.
|
|
||||||
If it is not present, add a new tab, call it "config.h"
|
|
||||||
and add the following variables:
|
|
||||||
char ssid[] = "ssid"; // your network SSID (name)
|
|
||||||
char pass[] = "password"; // your network password
|
|
||||||
|
|
||||||
based on SimpleGet example by Tom Igoe
|
based on SimpleGet example by Tom Igoe
|
||||||
header modifications by Todd Treece
|
header modifications by Todd Treece
|
||||||
|
modified 22 Jan 2019
|
||||||
|
by Tom Igoe
|
||||||
|
|
||||||
this example is in the public domain
|
this example is in the public domain
|
||||||
*/
|
*/
|
||||||
@ -29,8 +25,6 @@ int port = 8080;
|
|||||||
WiFiClient wifi;
|
WiFiClient wifi;
|
||||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||||
int status = WL_IDLE_STATUS;
|
int status = WL_IDLE_STATUS;
|
||||||
String response;
|
|
||||||
int statusCode = 0;
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
@ -53,7 +47,6 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|
||||||
Serial.println("making GET request");
|
Serial.println("making GET request");
|
||||||
client.beginRequest();
|
client.beginRequest();
|
||||||
client.get("/");
|
client.get("/");
|
||||||
@ -61,8 +54,8 @@ void loop() {
|
|||||||
client.endRequest();
|
client.endRequest();
|
||||||
|
|
||||||
// read the status code and body of the response
|
// read the status code and body of the response
|
||||||
statusCode = client.responseStatusCode();
|
int statusCode = client.responseStatusCode();
|
||||||
response = client.responseBody();
|
String response = client.responseBody();
|
||||||
|
|
||||||
Serial.print("GET Status code: ");
|
Serial.print("GET Status code: ");
|
||||||
Serial.println(statusCode);
|
Serial.println(statusCode);
|
||||||
@ -81,6 +74,8 @@ void loop() {
|
|||||||
client.sendHeader("X-CUSTOM-HEADER", "custom_value");
|
client.sendHeader("X-CUSTOM-HEADER", "custom_value");
|
||||||
client.endRequest();
|
client.endRequest();
|
||||||
client.write((const byte*)postData.c_str(), postData.length());
|
client.write((const byte*)postData.c_str(), postData.length());
|
||||||
|
// note: the above line can also be achieved with the simpler line below:
|
||||||
|
//client.print(postData);
|
||||||
|
|
||||||
// read the status code and body of the response
|
// read the status code and body of the response
|
||||||
statusCode = client.responseStatusCode();
|
statusCode = client.responseStatusCode();
|
||||||
|
@ -9,10 +9,8 @@
|
|||||||
|
|
||||||
For more on dweet.io, see https://dweet.io/play/
|
For more on dweet.io, see https://dweet.io/play/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
created 15 Feb 2016
|
created 15 Feb 2016
|
||||||
updated 16 Feb 2016
|
updated 22 Jan 2019
|
||||||
by Tom Igoe
|
by Tom Igoe
|
||||||
|
|
||||||
this example is in the public domain
|
this example is in the public domain
|
||||||
@ -33,8 +31,6 @@ String dweetName = "scandalous-cheese-hoarder"; // use your own thing name here
|
|||||||
WiFiClient wifi;
|
WiFiClient wifi;
|
||||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||||
int status = WL_IDLE_STATUS;
|
int status = WL_IDLE_STATUS;
|
||||||
int statusCode = 0;
|
|
||||||
String response;
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
@ -66,8 +62,8 @@ void loop() {
|
|||||||
client.get(path);
|
client.get(path);
|
||||||
|
|
||||||
// read the status code and body of the response
|
// read the status code and body of the response
|
||||||
statusCode = client.responseStatusCode();
|
int statusCode = client.responseStatusCode();
|
||||||
response = client.responseBody();
|
String response = client.responseBody();
|
||||||
Serial.print("Status code: ");
|
Serial.print("Status code: ");
|
||||||
Serial.println(statusCode);
|
Serial.println(statusCode);
|
||||||
Serial.print("Response: ");
|
Serial.print("Response: ");
|
||||||
|
@ -5,13 +5,8 @@
|
|||||||
|
|
||||||
Shows how to use Strings to assemble path and body
|
Shows how to use Strings to assemble path and body
|
||||||
|
|
||||||
note: WiFi SSID and password are stored in config.h file.
|
|
||||||
If it is not present, add a new tab, call it "config.h"
|
|
||||||
and add the following variables:
|
|
||||||
char ssid[] = "ssid"; // your network SSID (name)
|
|
||||||
char pass[] = "password"; // your network password
|
|
||||||
|
|
||||||
created 15 Feb 2016
|
created 15 Feb 2016
|
||||||
|
modified 22 Jan 2019
|
||||||
by Tom Igoe
|
by Tom Igoe
|
||||||
|
|
||||||
this example is in the public domain
|
this example is in the public domain
|
||||||
@ -31,8 +26,6 @@ int port = 80;
|
|||||||
WiFiClient wifi;
|
WiFiClient wifi;
|
||||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||||
int status = WL_IDLE_STATUS;
|
int status = WL_IDLE_STATUS;
|
||||||
int statusCode = 0;
|
|
||||||
String response;
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
@ -59,7 +52,6 @@ void loop() {
|
|||||||
// assemble the path for the POST message:
|
// assemble the path for the POST message:
|
||||||
String dweetName = "scandalous-cheese-hoarder";
|
String dweetName = "scandalous-cheese-hoarder";
|
||||||
String path = "/dweet/for/" + dweetName;
|
String path = "/dweet/for/" + dweetName;
|
||||||
|
|
||||||
String contentType = "application/json";
|
String contentType = "application/json";
|
||||||
|
|
||||||
// assemble the body of the POST message:
|
// assemble the body of the POST message:
|
||||||
@ -74,8 +66,8 @@ void loop() {
|
|||||||
client.post(path, contentType, postData);
|
client.post(path, contentType, postData);
|
||||||
|
|
||||||
// read the status code and body of the response
|
// read the status code and body of the response
|
||||||
statusCode = client.responseStatusCode();
|
int statusCode = client.responseStatusCode();
|
||||||
response = client.responseBody();
|
String response = client.responseBody();
|
||||||
|
|
||||||
Serial.print("Status code: ");
|
Serial.print("Status code: ");
|
||||||
Serial.println(statusCode);
|
Serial.println(statusCode);
|
||||||
|
@ -13,8 +13,6 @@
|
|||||||
This example shows how to concatenate Strings to assemble the
|
This example shows how to concatenate Strings to assemble the
|
||||||
PUT request and the body of the request.
|
PUT request and the body of the request.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
modified 15 Feb 2016
|
modified 15 Feb 2016
|
||||||
by Tom Igoe (tigoe) to match new API
|
by Tom Igoe (tigoe) to match new API
|
||||||
*/
|
*/
|
||||||
@ -98,5 +96,3 @@ void sendRequest(int light, String cmd, String value) {
|
|||||||
Serial.println(response);
|
Serial.println(response);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,12 +3,12 @@
|
|||||||
Connects to server once every five seconds, sends a POST request
|
Connects to server once every five seconds, sends a POST request
|
||||||
with custome headers and a request body
|
with custome headers and a request body
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
created 14 Feb 2016
|
created 14 Feb 2016
|
||||||
by Tom Igoe
|
by Tom Igoe
|
||||||
modified 18 Mar 2017
|
modified 18 Mar 2017
|
||||||
by Sandeep Mistry
|
by Sandeep Mistry
|
||||||
|
modified 22 Jan 2019
|
||||||
|
by Tom Igoe
|
||||||
|
|
||||||
this example is in the public domain
|
this example is in the public domain
|
||||||
*/
|
*/
|
||||||
@ -29,8 +29,6 @@ int port = 8080;
|
|||||||
WiFiClient wifi;
|
WiFiClient wifi;
|
||||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||||
int status = WL_IDLE_STATUS;
|
int status = WL_IDLE_STATUS;
|
||||||
String response;
|
|
||||||
int statusCode = 0;
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
@ -66,8 +64,8 @@ void loop() {
|
|||||||
client.endRequest();
|
client.endRequest();
|
||||||
|
|
||||||
// read the status code and body of the response
|
// read the status code and body of the response
|
||||||
statusCode = client.responseStatusCode();
|
int statusCode = client.responseStatusCode();
|
||||||
response = client.responseBody();
|
String response = client.responseBody();
|
||||||
|
|
||||||
Serial.print("Status code: ");
|
Serial.print("Status code: ");
|
||||||
Serial.println(statusCode);
|
Serial.println(statusCode);
|
||||||
|
@ -3,9 +3,8 @@
|
|||||||
Connects to server once every five seconds, sends a DELETE request
|
Connects to server once every five seconds, sends a DELETE request
|
||||||
and a request body
|
and a request body
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
created 14 Feb 2016
|
created 14 Feb 2016
|
||||||
|
modified 22 Jan 2019
|
||||||
by Tom Igoe
|
by Tom Igoe
|
||||||
|
|
||||||
this example is in the public domain
|
this example is in the public domain
|
||||||
@ -27,8 +26,6 @@ int port = 8080;
|
|||||||
WiFiClient wifi;
|
WiFiClient wifi;
|
||||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||||
int status = WL_IDLE_STATUS;
|
int status = WL_IDLE_STATUS;
|
||||||
String response;
|
|
||||||
int statusCode = 0;
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
@ -58,8 +55,8 @@ void loop() {
|
|||||||
client.del("/", contentType, delData);
|
client.del("/", contentType, delData);
|
||||||
|
|
||||||
// read the status code and body of the response
|
// read the status code and body of the response
|
||||||
statusCode = client.responseStatusCode();
|
int statusCode = client.responseStatusCode();
|
||||||
response = client.responseBody();
|
String response = client.responseBody();
|
||||||
|
|
||||||
Serial.print("Status code: ");
|
Serial.print("Status code: ");
|
||||||
Serial.println(statusCode);
|
Serial.println(statusCode);
|
||||||
|
@ -2,9 +2,8 @@
|
|||||||
Simple GET client for ArduinoHttpClient library
|
Simple GET client for ArduinoHttpClient library
|
||||||
Connects to server once every five seconds, sends a GET request
|
Connects to server once every five seconds, sends a GET request
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
created 14 Feb 2016
|
created 14 Feb 2016
|
||||||
|
modified 22 Jan 2019
|
||||||
by Tom Igoe
|
by Tom Igoe
|
||||||
|
|
||||||
this example is in the public domain
|
this example is in the public domain
|
||||||
@ -19,15 +18,12 @@
|
|||||||
char ssid[] = SECRET_SSID;
|
char ssid[] = SECRET_SSID;
|
||||||
char pass[] = SECRET_PASS;
|
char pass[] = SECRET_PASS;
|
||||||
|
|
||||||
|
|
||||||
char serverAddress[] = "192.168.0.3"; // server address
|
char serverAddress[] = "192.168.0.3"; // server address
|
||||||
int port = 8080;
|
int port = 8080;
|
||||||
|
|
||||||
WiFiClient wifi;
|
WiFiClient wifi;
|
||||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||||
int status = WL_IDLE_STATUS;
|
int status = WL_IDLE_STATUS;
|
||||||
String response;
|
|
||||||
int statusCode = 0;
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
@ -54,8 +50,8 @@ void loop() {
|
|||||||
client.get("/");
|
client.get("/");
|
||||||
|
|
||||||
// read the status code and body of the response
|
// read the status code and body of the response
|
||||||
statusCode = client.responseStatusCode();
|
int statusCode = client.responseStatusCode();
|
||||||
response = client.responseBody();
|
String response = client.responseBody();
|
||||||
|
|
||||||
Serial.print("Status code: ");
|
Serial.print("Status code: ");
|
||||||
Serial.println(statusCode);
|
Serial.println(statusCode);
|
||||||
|
@ -3,9 +3,8 @@
|
|||||||
Connects to server once every five seconds, sends a POST request
|
Connects to server once every five seconds, sends a POST request
|
||||||
and a request body
|
and a request body
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
created 14 Feb 2016
|
created 14 Feb 2016
|
||||||
|
modified 22 Jan 2019
|
||||||
by Tom Igoe
|
by Tom Igoe
|
||||||
|
|
||||||
this example is in the public domain
|
this example is in the public domain
|
||||||
@ -19,15 +18,12 @@
|
|||||||
char ssid[] = SECRET_SSID;
|
char ssid[] = SECRET_SSID;
|
||||||
char pass[] = SECRET_PASS;
|
char pass[] = SECRET_PASS;
|
||||||
|
|
||||||
|
|
||||||
char serverAddress[] = "192.168.0.3"; // server address
|
char serverAddress[] = "192.168.0.3"; // server address
|
||||||
int port = 8080;
|
int port = 8080;
|
||||||
|
|
||||||
WiFiClient wifi;
|
WiFiClient wifi;
|
||||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||||
int status = WL_IDLE_STATUS;
|
int status = WL_IDLE_STATUS;
|
||||||
String response;
|
|
||||||
int statusCode = 0;
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
@ -57,8 +53,8 @@ void loop() {
|
|||||||
client.post("/", contentType, postData);
|
client.post("/", contentType, postData);
|
||||||
|
|
||||||
// read the status code and body of the response
|
// read the status code and body of the response
|
||||||
statusCode = client.responseStatusCode();
|
int statusCode = client.responseStatusCode();
|
||||||
response = client.responseBody();
|
String response = client.responseBody();
|
||||||
|
|
||||||
Serial.print("Status code: ");
|
Serial.print("Status code: ");
|
||||||
Serial.println(statusCode);
|
Serial.println(statusCode);
|
||||||
|
@ -3,9 +3,8 @@
|
|||||||
Connects to server once every five seconds, sends a PUT request
|
Connects to server once every five seconds, sends a PUT request
|
||||||
and a request body
|
and a request body
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
created 14 Feb 2016
|
created 14 Feb 2016
|
||||||
|
modified 22 Jan 2019
|
||||||
by Tom Igoe
|
by Tom Igoe
|
||||||
|
|
||||||
this example is in the public domain
|
this example is in the public domain
|
||||||
@ -19,15 +18,12 @@
|
|||||||
char ssid[] = SECRET_SSID;
|
char ssid[] = SECRET_SSID;
|
||||||
char pass[] = SECRET_PASS;
|
char pass[] = SECRET_PASS;
|
||||||
|
|
||||||
|
|
||||||
char serverAddress[] = "192.168.0.3"; // server address
|
char serverAddress[] = "192.168.0.3"; // server address
|
||||||
int port = 8080;
|
int port = 8080;
|
||||||
|
|
||||||
WiFiClient wifi;
|
WiFiClient wifi;
|
||||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||||
int status = WL_IDLE_STATUS;
|
int status = WL_IDLE_STATUS;
|
||||||
String response;
|
|
||||||
int statusCode = 0;
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
@ -57,8 +53,8 @@ void loop() {
|
|||||||
client.put("/", contentType, putData);
|
client.put("/", contentType, putData);
|
||||||
|
|
||||||
// read the status code and body of the response
|
// read the status code and body of the response
|
||||||
statusCode = client.responseStatusCode();
|
int statusCode = client.responseStatusCode();
|
||||||
response = client.responseBody();
|
String response = client.responseBody();
|
||||||
|
|
||||||
Serial.print("Status code: ");
|
Serial.print("Status code: ");
|
||||||
Serial.println(statusCode);
|
Serial.println(statusCode);
|
||||||
|
@ -3,9 +3,10 @@
|
|||||||
Connects to the WebSocket server, and sends a hello
|
Connects to the WebSocket server, and sends a hello
|
||||||
message every 5 seconds
|
message every 5 seconds
|
||||||
|
|
||||||
|
|
||||||
created 28 Jun 2016
|
created 28 Jun 2016
|
||||||
by Sandeep Mistry
|
by Sandeep Mistry
|
||||||
|
modified 22 Jan 2019
|
||||||
|
by Tom Igoe
|
||||||
|
|
||||||
this example is in the public domain
|
this example is in the public domain
|
||||||
*/
|
*/
|
||||||
@ -77,4 +78,3 @@ void loop() {
|
|||||||
|
|
||||||
Serial.println("disconnected");
|
Serial.println("disconnected");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user