HTTP Headers

Read HTTP headers from a response

You have a direct access to the HTTP headers in the response from the server:

 local response = http.request_batch({
     {"GET", ""},
 if client.get_id()  1 and client.get_repetition()  1 then -- Let client 1 do it, and only once
     " uses `" .. tostring(response[1].headers["Server"][1]) .. "' as a server software"


Setting headers for a request

Sometimes you need to set custom HTTP headers for your request. One reason is to see if your server handles client-side caching.

If the web browser already has the requested resource in its cache, it may send a If-Modified-Since header, telling the server that it only wants the server to return the resource if it has been changed since last time the browser fetched it. When server sees this header, it may respond “304 Not modified”, instead of returning the actual content, thus saving bandwidth.

(Note that some web servers use an exact match for the modification date check instead of a “less-than” operation (this is explicitly allowed by the standard). So you have to send the exact date you received in the “Last-Modified” header from the server last time you requested the resource)

In the example we assume that you do know the exact page modification date.

 local response = http.request_batch({
    {"GET", "",
        headers = {
            ["If-Modified-Since"] = "Tue, 27 Apr 2010 03:30:00 GMT";
 if response[1].status_code==304 then"not modified")
 elseif response[1].status_code==200 then"modified")
     log.error("unexpected code" .. tostring(response[1].status_code))

(Note that you may want to additionally test the support for “If-None-Match” header)

See also:

Feedback and Knowledge Base