Skip to content

Commit 767ecf2

Browse files
agdlsandeepmistry
authored andcommitted
Added Custom Header Example (arduino-libraries#20)
Added the example posted in arduino-libraries#12 to close the PR. Tested and it works
1 parent 4e21c84 commit 767ecf2

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Custom request header example for the ArduinoHttpClient
3+
library. This example sends a GET and a POST request with a custom header every 5 seconds.
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+
based on SimpleGet example by Tom Igoe
12+
header modifications by Todd Treece
13+
14+
this example is in the public domain
15+
*/
16+
17+
#include <ArduinoHttpClient.h>
18+
#include <WiFi101.h>
19+
#include "config.h"
20+
21+
char serverAddress[] = "192.168.0.3"; // server address
22+
int port = 8080;
23+
24+
WiFiClient wifi;
25+
HttpClient client = HttpClient(wifi, serverAddress, port);
26+
int status = WL_IDLE_STATUS;
27+
String response;
28+
int statusCode = 0;
29+
30+
void setup() {
31+
Serial.begin(9600);
32+
while ( status != WL_CONNECTED) {
33+
Serial.print("Attempting to connect to Network named: ");
34+
Serial.println(ssid); // print the network name (SSID);
35+
36+
// Connect to WPA/WPA2 network:
37+
status = WiFi.begin(ssid, pass);
38+
}
39+
40+
// print the SSID of the network you're attached to:
41+
Serial.print("SSID: ");
42+
Serial.println(WiFi.SSID());
43+
44+
// print your WiFi shield's IP address:
45+
IPAddress ip = WiFi.localIP();
46+
Serial.print("IP Address: ");
47+
Serial.println(ip);
48+
}
49+
50+
void loop() {
51+
52+
Serial.println("making GET request");
53+
client.beginRequest();
54+
client.get("/");
55+
client.sendHeader("X-CUSTOM-HEADER", "custom_value");
56+
client.endRequest();
57+
58+
// read the status code and body of the response
59+
statusCode = client.responseStatusCode();
60+
response = client.responseBody();
61+
62+
Serial.print("GET Status code: ");
63+
Serial.println(statusCode);
64+
Serial.print("GET Response: ");
65+
Serial.println(response);
66+
67+
Serial.println("Wait five seconds");
68+
delay(5000);
69+
70+
Serial.println("making POST request");
71+
String postData = "name=Alice&age=12";
72+
client.beginRequest();
73+
client.post("/");
74+
client.sendHeader(HTTP_HEADER_CONTENT_TYPE, "application/x-www-form-urlencoded");
75+
client.sendHeader(HTTP_HEADER_CONTENT_LENGTH, postData.length());
76+
client.sendHeader("X-CUSTOM-HEADER", "custom_value");
77+
client.endRequest();
78+
client.write((const byte*)postData.c_str(), postData.length());
79+
80+
// read the status code and body of the response
81+
statusCode = client.responseStatusCode();
82+
response = client.responseBody();
83+
84+
Serial.print("POST Status code: ");
85+
Serial.println(statusCode);
86+
Serial.print("POST Response: ");
87+
Serial.println(response);
88+
89+
Serial.println("Wait five seconds");
90+
delay(5000);
91+
}

examples/CustomHeader/config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
char ssid[] = "ssid"; // your network SSID (name)
2+
char pass[] = "password"; // your network password
3+

0 commit comments

Comments
 (0)