What is the appropriate http code to respond to the contents of an image?

Asked

Viewed 116 times

1

Well, the question is quite straightforward.

I want to use PHP to answer the content of an image. However, I would like to know if the http status type will have any impact on the client (for example, caching images in the browser).

In this case, I’m using Laravel 3

$cachedImage = 'storage/images/cached.jpg';

$code = ... // Qual deles usar?

return Response::make(File::get($cachedImage), $code, ['content-type' => 'image/jpeg']);

I should use the code 200 or 304 ? I realized that in Google Chrome, is returning 304 for images!

  • 1

    the normal is code 200 unless you want to send a specific status. If you want to use an image to represent a page not found, you can use 404, for example.

  • But in this case, I need a status that represents an existing image, but it should be returned by PHP.

  • Ué.. then it is 200 rsrss

2 answers

1


For that there are the RFC, are technical documents developed and maintained by the IETF (Internet Enginnering Task Force), an institution that specifies the standards that will be implemented and used throughout the Internet.

According to the RFC 2616, section 10:

10.3.5 304 Not Modified

If the client has performed a conditional GET request and access is allowed, but the Document has not been modified, the server SHOULD Respond with this status code. The 304 Response MUST NOT contain a message-body, and Thus is Always terminated by the first Empty line after the header Fields.

The Response MUST include the following header Fields:

  • Date, unless its omission is required by Section 14.18.1 If a clockless origin server Obeys These Rules, and proxies and clients add their Own Date to any Response Received without one (as already specified by [RFC 2068], Section 14.19), caches will Operate correctly.

  • Etag and/or Content-Location, if the header would have been sent in a 200 Sponse to the same request

  • Expires, Cache-Control, and/or Vary, if the field-value Might differ from that sent in any Previous Response for the same Variant

If the conditional GET used a Strong cache Validator (see Section 13.3.3), the Response SHOULD NOT include other Entity-headers. Otherwise (i.e., the conditional GET used a Weak Validator), the Response MUST NOT include other Entity-headers; this prevents inconsistencies between cached Entity-bodies and updated headers.

If a 304 Response indicates an Entity not Currently cached, then the cache MUST disregard the Response and repeat the request without the conditional.

If a cache uses a Received 304 Response to update a cache entry, the cache MUST update the entry to reflect any new field values Given in the Response.

In short

For your answer, you can use yes 304 since the user request will not modify anything on the server and you are just returning the image.


Sources:

0

In fact, for image, I’ve never seen use any! The code in this case is implicitly 200, so it would be redundant to insert 200.

The most important is the header you are already inserting.

  • 1

    HTTP Answer without code? F12 -> Network...

  • How are you using Laravel: YES, without explicitly Answer already returns 200 as default.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.