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

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

问题描述

是否可以通过 HTTP 请求发送标头,告诉 Web 服务器仅向您发送标头作为响应而不发送正文内容?

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天全站免登陆