发送一个HTTP请求,告诉服务器只返回标题而没有正文? [英] Sending a HTTP request that tells server to return only headers and no body?

查看:128
本文介绍了发送一个HTTP请求,告诉服务器只返回标题而没有正文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您是否可以通过HTTP请求发送标题,告知网络服务器仅向您发送回复标题但没有正文内容?

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?

我正在使用cURL提出此类请求,但从技术上讲,我认为应该可以将简单的标头作为请求的一部分发送。

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.

例如,存在304 Not Modified HTTP Response Code 。当您使用缓存标记或日期时间信息向服务器发出请求时,服务器可以检测到该请求,并仅返回标头作为响应,告知用户代理将其缓存用作响应。这意味着来自服务器的响应在带宽方面非常非常小。

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?

这里的想法是发出HTTP请求,否则可能返回大量数据,非常小。例如,将返回JSON数据或日志的API调用,实际上用户代理唯一感兴趣的是确保请求通过而没有其他任何内容。这是为了在主体可能存在的情况下最小化进出服务器的带宽,但在用户代理正在进行的任何情况下都不需要。

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.

虽然可以设计一个能够侦听特定自定义标题或其他东西的API,但除非必须这样做,否则我宁愿不这样做。但我一直无法找到是否有一种标准化的方式来发出HTTP请求,告诉服务器不要发送任何正文内容?

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?

推荐答案

如果服务器支持,则有 HEAD 操作(与GET或POST相反)。这告诉服务器只发送标题,没有正文。

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.


HEAD方法与GET相同,但服务器必须不是
在响应中返回一个消息正文。

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

这篇关于发送一个HTTP请求,告诉服务器只返回标题而没有正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆