phphttphttp-headersslimslim-2

Slim Framework 2 - Not getting HTTP 304 code with etag()


I am using slim framework 2 etag() to fetch cache data, all working fine i am getting the cache data with the postman or any rest client but i am getting HTTP 200 OK response all time in the rest client but as it should be HTTP 304 when the data comes from cache

below is my slim api:

 $app->get('/getNew', function () use ($app){
     $app->etag('uniqueEtag12');
    echo "I am updated one";
  });

I don't have any idea why every time i am getting 200 OK response code in the rest client as i am getting the cache data in the response, below is my rest client response snap

enter image description here

why this status code always 200 OK why i am not getting status code 304 please help me


Solution

  • The etag() method does two things:

    1. Add the ETag header to the response
    2. Send a 304 status response if the client sends an If-None-Match header with the value uniqueEtag12

    Therefore, I think that your request does not include an If-None-Match header.

    Example code:

    index.php:

    <?php
    require 'vendor/autoload.php';
    
    $app = new \Slim\Slim();
    
    $app->get('/hello', function () use ($app) {
        $app->etag('1234');
        echo "Hello world on " . date("Y-m-d H:i:s");
    });
    
    
    $app->run();
    

    Test using curl.

    No If-None-Match header: $ curl -i http://localhost:8888/hello

    HTTP/1.1 200 OK
    Host: localhost:8888
    Connection: close
    X-Powered-By: PHP/7.0.15
    Content-type: text/html;charset=UTF-8
    Etag: "1234"
    
    Hello world on 2017-05-04 07:12:40
    

    With an If-None-Match header:

    $ curl -i http://localhost:8888/hello -H 'If-None-Match: "1234"'
    HTTP/1.1 304 Not Modified
    Host: localhost:8888
    Connection: close
    X-Powered-By: PHP/7.0.15
    Etag: "1234"