Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 917
kingmaple Avatar asked Aug 01 '12 07:08

kingmaple


People also ask

Is it possible to make an HTTP request with empty header value?

HTTP headers are key/value pairs sent at the beginning of a request or response. According to the grammar in RFC 7230, a field could have an empty value. In practice, it probably doesn't make much sense: semantically, a header with an empty value or the absence of that header are equivalent.

Does every HTTP response have a body?

Most HTTP requests are GET requests without bodies. However, simulating requests with bodies is important to properly stress the proxy code and to test various hooks working with such requests. Most HTTP requests with bodies use POST or PUT request method.

What is a HTTP request header?

A request header is an HTTP header that can be used in an HTTP request to provide information about the request context, so that the server can tailor the response. For example, the Accept-* headers indicate the allowed and preferred formats of the response.


2 Answers

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.

like image 168
Ikke Avatar answered Sep 21 '22 14:09

Ikke


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)?

like image 22
CodeCaster Avatar answered Sep 20 '22 14:09

CodeCaster