Sending an HTTP request that tells the server that it returns only headers and body?

Are there any headers that you can send using an HTTP request that tells the web server to send you only headers in response and without body content?

I use cURL to execute such requests, but technically I assume that this should be possible when a simple header is sent as part of the request.

For example, there is a 304 Not Modified HTTP Response Code. When you send a request to the server with a cache tag or time and time information, the server can detect this and return headers as a response that says the user agent uses its cache as the response. This means that the response from the server is very, very small when it comes to bandwidth.

But is there a header that you can send to servers that force the server to return the header without the contents of the body?

The idea here is to make HTTP requests that would otherwise return a large amount of data, very small. For example, an API call that will return JSON data or a log, if in fact the user agent is the only one interested in making sure the request passes, and nothing more. This is done in order to minimize throughput on the server and from the server in those cases where the body can exist, but not necessarily in any way in the context of what the user agent does.

Although it would be possible to develop an API that would listen on a specific user-defined header or something else, I would prefer not to go this route unless I have to. But I could not find if there is a standardized way to make an HTTP request that tells the server not to send any body content?

+7
source share
2 answers

If the server supports it, there is a HEAD action (unlike GET or POST). This tells the server that it sends only headers, not the body.

The HEAD method is identical to GET, except that the server SHOULD NOT return the message body in response.

+14
source

You are looking for the verb HEAD . If you use CURL with PHP, this will do the trick:

 curl_setopt( $c, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $c, CURLOPT_CUSTOMREQUEST, 'HEAD' ); curl_setopt( $c, CURLOPT_HEADER, 1 ); curl_setopt( $c, CURLOPT_NOBODY, true ); 

If you use PHP though (and this can very well be taken into account in different situations), the script will continue to work until any output is sent (note in the REQUEST_METHOD section). Therefore, if you are worried about server stress, you may need to request something else, if that is appropriate.

It seems to me that you are more looking for "ping", so why not just request a file that does nothing and return some kind of OK, maybe even just HTTP 200 OK (i.e. an empty file)

+2
source

All Articles