CustomHeader.ino 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. note: WiFi SSID and password are stored in config.h file.
  5. If it is not present, add a new tab, call it "config.h"
  6. and add the following variables:
  7. char ssid[] = "ssid"; // your network SSID (name)
  8. char pass[] = "password"; // your network password
  9. based on SimpleGet example by Tom Igoe
  10. header modifications by Todd Treece
  11. this example is in the public domain
  12. */
  13. #include <ArduinoHttpClient.h>
  14. #include <WiFi101.h>
  15. #include "config.h"
  16. char serverAddress[] = "192.168.0.3"; // server address
  17. int port = 8080;
  18. WiFiClient wifi;
  19. HttpClient client = HttpClient(wifi, serverAddress, port);
  20. int status = WL_IDLE_STATUS;
  21. String response;
  22. int statusCode = 0;
  23. void setup() {
  24. Serial.begin(9600);
  25. while ( status != WL_CONNECTED) {
  26. Serial.print("Attempting to connect to Network named: ");
  27. Serial.println(ssid); // print the network name (SSID);
  28. // Connect to WPA/WPA2 network:
  29. status = WiFi.begin(ssid, pass);
  30. }
  31. // print the SSID of the network you're attached to:
  32. Serial.print("SSID: ");
  33. Serial.println(WiFi.SSID());
  34. // print your WiFi shield's IP address:
  35. IPAddress ip = WiFi.localIP();
  36. Serial.print("IP Address: ");
  37. Serial.println(ip);
  38. }
  39. void loop() {
  40. Serial.println("making GET request");
  41. client.beginRequest();
  42. client.get("/");
  43. client.sendHeader("X-CUSTOM-HEADER", "custom_value");
  44. client.endRequest();
  45. // read the status code and body of the response
  46. statusCode = client.responseStatusCode();
  47. response = client.responseBody();
  48. Serial.print("GET Status code: ");
  49. Serial.println(statusCode);
  50. Serial.print("GET Response: ");
  51. Serial.println(response);
  52. Serial.println("Wait five seconds");
  53. delay(5000);
  54. Serial.println("making POST request");
  55. String postData = "name=Alice&age=12";
  56. client.beginRequest();
  57. client.post("/");
  58. client.sendHeader(HTTP_HEADER_CONTENT_TYPE, "application/x-www-form-urlencoded");
  59. client.sendHeader(HTTP_HEADER_CONTENT_LENGTH, postData.length());
  60. client.sendHeader("X-CUSTOM-HEADER", "custom_value");
  61. client.endRequest();
  62. client.write((const byte*)postData.c_str(), postData.length());
  63. // read the status code and body of the response
  64. statusCode = client.responseStatusCode();
  65. response = client.responseBody();
  66. Serial.print("POST Status code: ");
  67. Serial.println(statusCode);
  68. Serial.print("POST Response: ");
  69. Serial.println(response);
  70. Serial.println("Wait five seconds");
  71. delay(5000);
  72. }