2
2
Custom request header example for the ArduinoHttpClient
3
3
library. This example sends a GET and a POST request with a custom header every 5 seconds.
4
4
5
- note: WiFi SSID and password are stored in config.h file.
6
- If it is not present, add a new tab, call it "config.h"
7
- and add the following variables:
8
- char ssid[] = "ssid"; // your network SSID (name)
9
- char pass[] = "password"; // your network password
10
-
11
5
based on SimpleGet example by Tom Igoe
12
6
header modifications by Todd Treece
7
+ modified 22 Jan 2019
8
+ by Tom Igoe
13
9
14
10
this example is in the public domain
15
- */
16
-
11
+ */
12
+
17
13
#include < ArduinoHttpClient.h>
18
14
#include < WiFi101.h>
19
15
@@ -29,8 +25,6 @@ int port = 8080;
29
25
WiFiClient wifi;
30
26
HttpClient client = HttpClient(wifi, serverAddress, port);
31
27
int status = WL_IDLE_STATUS;
32
- String response;
33
- int statusCode = 0 ;
34
28
35
29
void setup () {
36
30
Serial.begin (9600 );
@@ -53,16 +47,15 @@ void setup() {
53
47
}
54
48
55
49
void loop () {
56
-
57
50
Serial.println (" making GET request" );
58
51
client.beginRequest ();
59
52
client.get (" /" );
60
53
client.sendHeader (" X-CUSTOM-HEADER" , " custom_value" );
61
54
client.endRequest ();
62
55
63
56
// read the status code and body of the response
64
- statusCode = client.responseStatusCode ();
65
- response = client.responseBody ();
57
+ int statusCode = client.responseStatusCode ();
58
+ String response = client.responseBody ();
66
59
67
60
Serial.print (" GET Status code: " );
68
61
Serial.println (statusCode);
@@ -81,6 +74,8 @@ void loop() {
81
74
client.sendHeader (" X-CUSTOM-HEADER" , " custom_value" );
82
75
client.endRequest ();
83
76
client.write ((const byte*)postData.c_str (), postData.length ());
77
+ // note: the above line can also be achieved with the simpler line below:
78
+ // client.print(postData);
84
79
85
80
// read the status code and body of the response
86
81
statusCode = client.responseStatusCode ();
0 commit comments