The HEAD and OPTIONS methods help clients learn about a web resource without getting the full content. They make communication faster and clearer.
0
0
HEAD and OPTIONS methods in Rest API
Introduction
To check if a web page or file exists without downloading it.
To find out what actions (like GET, POST) a server allows on a resource.
To get metadata like size or last update time of a resource without the full data.
To test if a server is reachable and responding before making a full request.
Syntax
Rest API
HEAD /resource HTTP/1.1 Host: example.com OPTIONS /resource HTTP/1.1 Host: example.com
The HEAD method requests headers only, no body content.
The OPTIONS method asks the server which HTTP methods it supports for a resource.
Examples
Check if
index.html exists and get its headers like content length.Rest API
HEAD /index.html HTTP/1.1
Host: example.com
Ask the server which methods are allowed on
/api/data.Rest API
OPTIONS /api/data HTTP/1.1
Host: example.com
Sample Program
This Python program sends a HEAD request to check headers of /index.html and an OPTIONS request to see allowed methods. It prints the status codes and headers received.
Rest API
import http.client conn = http.client.HTTPConnection('example.com') # Using HEAD method conn.request('HEAD', '/index.html') response = conn.getresponse() print('HEAD status:', response.status) print('HEAD headers:', response.getheaders()) # Using OPTIONS method conn.request('OPTIONS', '/index.html') response = conn.getresponse() print('OPTIONS status:', response.status) print('Allow header:', response.getheader('Allow')) conn.close()
OutputSuccess
Important Notes
HEAD responses have the same headers as GET but no body content.
OPTIONS responses include an Allow header listing supported methods.
These methods help reduce data usage and improve efficiency.
Summary
HEAD checks resource headers without downloading content.
OPTIONS asks what methods a server supports on a resource.
Both methods help clients communicate efficiently with servers.