Master The Pico WiFi: Simplest HTTP Client |
Written by Harry Fairhead & Mike James | |||||
Tuesday, 11 April 2023 | |||||
Page 3 of 4
An HTTP ClientAs a first example we can construct the simplest possible HTTP client. In the sake of simplicity no error checking is performed and only the callbacks that are needed are defined. This is as simple as it gets and a starting point for a more complete client. The most basic transaction the client can have with the server is to send a GET request for the server to send back a particular file. Thus the simplest header is: "GET /index.html HTTP/1.1\r\n\r\n" which is a request for the server to send index.html. In most cases we need one more header, HOST, which gives the domain name of the server. Why do we need it? Simply because HTTP says you should and many websites are hosted by a single server at the same IP address. Which website the server retrieves the file from is governed by the domain name you specify in the HOST header. This means that the simplest set of headers we can send the server is: "GET /index.htm HTTP/1.1\r\nHOST:example.org\r\n\r\" which corresponds to the headers: GET /index.html HTTP/1.1 HOST:example.org An HTTP request always ends with a blank line. If you don't send the blank line then you will get no response from most servers. In addition, the HOST header has to have the domain name with no additional syntax - no slashes and no http: or similar. #define BUF_SIZE 2048 char myBuff[BUF_SIZE]; char header[] = "GET /index.html HTTP/1.1\r\nHOST:example.com\r\n\r\n"; Now we are ready to send our request to the server and the first thing we need is a PCB set to call a recv callback: struct tcp_pcb *pcb = tcp_new(); In this case we only need to define a callback to handle the received data. Now we have a PCB we can connect to the server: ip_addr_t ip; Now all we have to do is define the two callbacks. The connected callback is used to send the headers to the server: static err_t connected(void *arg, struct tcp_pcb *pcb, The recv callback simply checks to see that there really is something to read and then displays some statistics, converts the PBUF to a character array and displays the content. Finally it reports that the data has been received and frees the PBUF. err_t recv(void *arg, struct tcp_pcb *pcb, This is a very simple receive function and it doesn’t make any attempt to deal with empty responses from the server or to close the connection. Usually an HTTP 1.1 connection is left open even after the requested data has been transmitted by the server just in case it can be reused by the client for another request. In this case the connection eventually times out. You can include an additional header Connection: close to ask the server not to keep the connection open but some will ignore this. The complete program is: #include <stdio.h> |
|||||
Last Updated ( Tuesday, 11 April 2023 ) |