Sending a HTTP request that tells server to return only headers and no body?

11,764

Solution 1

If the server supports it, there is the HEAD action (as opposed to GET or POST). That tells the server to only send the headers, and no body.

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response.

Solution 2

You're looking for the verb HEAD. If you're using 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 are using PHP though (and it may very well also count in different situations), the script will continue to run until any output is sent (the note under REQUEST_METHOD). So if you're worried about the server stress, you might require something else if that fits.

To me it looks like you're more looking for a 'ping', so why not simply request a file that does nothing but return an OK of some kind, perhaps even just the HTTP 200 OK (i.e. an empty file)?

Share:
11,764

Related videos on Youtube

kingmaple
Author by

kingmaple

I am natural born Aries, I went through military service and obtained a degree in the University of Tallinn, in Estonia. I am working as an architect in the fields of internet media, creating applications like the one you are using right now. I also consult developers and startups, so if you need help with anything, let me know.

Updated on June 05, 2022

Comments

  • kingmaple
    kingmaple almost 2 years

    Are there headers you can send with a HTTP request that tell the web server to send you only headers in response and no body content?

    I am using cURL to make such requests, but technically I assume it should be possible with simple header being sent as part of the request.

    For example, there exists a 304 Not Modified HTTP Response Code. When you make a request to the server with your cache tag or datetime information, then the server can detect that and only return headers as a response that tell the user agent to use their 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 you can send to servers that make the server only return a header and no body content?

    The idea here is to make HTTP requests that otherwise might return large amount of data, very small. Such as an API call that would return JSON data or a log, when in fact the only thing user agent is interested in is making sure that the request went through and nothing else. This is in order to minimize the bandwidth to and from the server in cases where the body might exist, but is not necessary in any way in context to what the user agent is doing.

    While it would be possible to design an API that listens to a specific custom header or whatnot, I'd rather not go this way unless I have to. But I've been unable to find if there is a standardized way to make a HTTP request that tells server not to send any body content?