|
| 1 | + |
| 2 | +ESP8266 Web Server |
| 3 | +================== |
| 4 | + |
| 5 | +The WebServer class found in ``ESP8266WebServer.h`` header, is a simple web server that knows how to handle HTTP requests such as GET and POST and can only support one simultaneous client. |
| 6 | + |
| 7 | +Usage |
| 8 | +----- |
| 9 | + |
| 10 | +Class Constructor |
| 11 | +~~~~~~~~~~~~~~~~~ |
| 12 | + |
| 13 | +.. code:: cpp |
| 14 | +
|
| 15 | + ESP8266WebServer server(80); |
| 16 | +
|
| 17 | +Creates the ESP8266WebServer class object. |
| 18 | + |
| 19 | +*Parameters:* |
| 20 | + |
| 21 | +host IP address: ``IPaddress addr`` (optional) |
| 22 | + |
| 23 | +host port number: ``int port`` (default is the standard HTTP port 80) |
| 24 | + |
| 25 | +Basic Operations |
| 26 | +~~~~~~~~~~~~~~~~ |
| 27 | + |
| 28 | +Starting the server |
| 29 | +^^^^^^^^^^^^^^^^^^^ |
| 30 | + |
| 31 | +.. code:: cpp |
| 32 | +
|
| 33 | + void begin(); |
| 34 | +
|
| 35 | +Handling incoming client requests |
| 36 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 37 | + |
| 38 | +.. code:: cpp |
| 39 | +
|
| 40 | + void handleClient(); |
| 41 | +
|
| 42 | +Disabling the server |
| 43 | +^^^^^^^^^^^^^^^^^^^^ |
| 44 | + |
| 45 | +.. code:: cpp |
| 46 | +
|
| 47 | + void close(); |
| 48 | + void stop(); |
| 49 | +
|
| 50 | +Both methods function the same |
| 51 | + |
| 52 | +Client request handlers |
| 53 | +^^^^^^^^^^^^^^^^^^^^^^^ |
| 54 | + |
| 55 | +.. code:: cpp |
| 56 | +
|
| 57 | + void on(); |
| 58 | + void addHandler(); |
| 59 | + void onNotFound(); |
| 60 | + void onFileUpload(); |
| 61 | +
|
| 62 | +*Example:* |
| 63 | + |
| 64 | +.. code:: cpp |
| 65 | +
|
| 66 | + server.on("/", handlerFunction); |
| 67 | + server.onNotFound(handlerFunction); // called when handler is not assigned |
| 68 | + server.onFileUpload(handlerFunction); // handle file uploads |
| 69 | +
|
| 70 | +Sending responses to the client |
| 71 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 72 | + |
| 73 | +.. code:: cpp |
| 74 | +
|
| 75 | + void send(); |
| 76 | + void send_P(); |
| 77 | +
|
| 78 | +*Parameters:* |
| 79 | + |
| 80 | +``code`` - HTTP response code, can be ``200`` or ``404``, etc. |
| 81 | + |
| 82 | +``content_type`` - HTTP content type, like ``"text/plain"`` or ``"image/png"``, etc. |
| 83 | + |
| 84 | +``content`` - actual content body |
| 85 | + |
| 86 | +Advanced Options |
| 87 | +~~~~~~~~~~~~~~~~ |
| 88 | + |
| 89 | +Getting information about request arguments |
| 90 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 91 | + |
| 92 | +.. code:: cpp |
| 93 | +
|
| 94 | + const String & arg(); |
| 95 | + const String & argName(); |
| 96 | + int args(); |
| 97 | + bool hasArg(); |
| 98 | +
|
| 99 | +``arg`` - get request argument value |
| 100 | + |
| 101 | +``argName`` - get request argument name |
| 102 | + |
| 103 | +``args`` - get arguments count |
| 104 | + |
| 105 | +``hasArg`` - check if argument exist |
| 106 | + |
| 107 | +Getting information about request headers |
| 108 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 109 | + |
| 110 | +.. code:: cpp |
| 111 | +
|
| 112 | + const String & header(); |
| 113 | + const String & headerName(); |
| 114 | + const String & hostHeader(); |
| 115 | + int headers(); |
| 116 | + bool hasHeader(); |
| 117 | + |
| 118 | +
|
| 119 | +``header`` - get request header value |
| 120 | + |
| 121 | +``headerName`` - get request header name |
| 122 | + |
| 123 | +``hostHeader`` - get request host header if available, else empty string |
| 124 | + |
| 125 | +``headers`` - get header count |
| 126 | + |
| 127 | +``hasHeader`` - check if header exist |
| 128 | + |
| 129 | +Authentication |
| 130 | +^^^^^^^^^^^^^^ |
| 131 | + |
| 132 | +.. code:: cpp |
| 133 | +
|
| 134 | + bool authenticate(); |
| 135 | + void requestAuthentication(); |
| 136 | +
|
| 137 | +``authenticate`` - server authentication, returns true if client is authenticated else false |
| 138 | + |
| 139 | +``requestAuthentication`` - sends authentication failure response to the client |
| 140 | + |
| 141 | +*Example Usage:* |
| 142 | + |
| 143 | +.. code:: cpp |
| 144 | +
|
| 145 | + if(!server.authenticate(username, password)){ |
| 146 | + server.requestAuthentication(); |
| 147 | + } |
| 148 | +
|
| 149 | +
|
| 150 | +Other Function Calls |
| 151 | +~~~~~~~~~~~~~~~~~~~~ |
| 152 | + |
| 153 | +.. code:: cpp |
| 154 | +
|
| 155 | + const String & uri(); // get the current uri |
| 156 | + HTTPMethod method(); // get the current method |
| 157 | + WiFiClient client(); // get the current client |
| 158 | + HTTPUpload & upload(); // get the current upload |
| 159 | + void setContentLength(); // set content length |
| 160 | + void sendHeader(); // send HTTP header |
| 161 | + void sendContent(); // send content |
| 162 | + void sendContent_P(); |
| 163 | + void collectHeaders(); // set the request headers to collect |
| 164 | + void serveStatic(); |
| 165 | + size_t streamFile(); |
| 166 | +
|
| 167 | +For code samples enter `here <https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WebServer/examples>`__ . |
| 168 | + |
0 commit comments