abhir24:
I am new to http. Can you please explain what you meant by location header? Or, rather tell me some reference where I can learn this from?
HTTP stands for HyperText Transfer Protocol, meaning that it is text-based. There are two important HTTP request methods: GET and POST. Let's focus on GET for now. A client (e.g. Arduino) sends a GET request to an HTTP server (e.g. https://www.arduino.cc). The request only contains a header: this is a piece of text that tells the server what to do. The header has two parts:
- The request line:
GET /latest.txt HTTP/1.1
It tells the server what method is used, what URI is requested, and what version of the HTTP protocol is used.
The URI (Uniform Resource Identifier) points to a unique file in the server's file system, this is handled by the web server. (A URI doesn't have to be a file, but for most web servers, it is.)
2. The header fields (sometimes called "headers"):
Host: www.arduino.cc
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
DNT: 1
These fields contain some extra information for the server: for example, the host name of the server, what kind of user agent made the request, what kind of response file types the client accepts, and so on.
Generally, these header fields are of the format "key: value". Each header field is followed by a carriage return and a line feed (\r\n). An empty line (\r\n\r\n) indicates the end of the header.
When the server processes the request, and it's able to execute that request without problems, it will respond with an HTTP status code of 200 (Ok):
HTTP/1.1 200 OK
This is then followed by the response header fields:
Date: Tue, 06 Jun 2017 09:53:05 GMT
Last-Modified: Wed, 01 Sep 2004 13:24:52 GMT
ETag: "277f-3e3073913b100-gzip"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Type:text/html; charset=UTF-8
Content-Encoding: gzip
Cache-Control: max-age=21600
Expires: Tue, 06 Jun 2017 15:53:05 GMT
These headers contain some information about the server, and some information about the file, for example, the date it was last modified, the file type, how the file was encoded, some cache control data, and so on.
Again, an empty line (\r\n\r\n) indicates the end of the header. After the header, the actual response or file is sent.
However, if the request could not be processed, a status code other than 200 will be sent. Section 10 of the HTTP/1.1 specification tells you what to do in case of these status codes.
For example, if the server responds with
HTTP/1.1 301 Moved Permanently
Content-Type: text/html
Date: Fri, 02 Jun 2017 06:17:32 GMT
Location: https://www.arduino.cc/latest.txt
Server: nginx/1.4.2
Content-Length: 184
Connection: Close
This means that the file has been moved, and that you have to send a new GET request to the new URL, that is specified in the Location header field (https://www.arduino.cc/latest.txt, in this case).
Pieter