API methods RESTful; HEAD AND OPTIONS

I am writing a RESTful API module for an application in PHP, and I am a bit mixed up with the verbs HEAD and OPTIONS .

  • OPTIONS Used to get available HTTP verbs for this resource?
  • HEAD Used to determine if a given resource is available.

If someone can clarify * these verbs, that would be greatly appreciated.

* Clarification has been made regarding RESTful API architectures re-targeting HTTP verbs. Since then, I realized that both HEAD and OPTIONS should not be reassigned, but instead behave predictably, like any HTTP application. Oh, how we develop in 2 years.

+51
rest api php
Jul 12 2018-11-11T00:
source share
3 answers

According to: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

9.2 OPTIONS

The OPTIONS method presents a request for information about the communication options available in the request / response chain identified by the Request-URI. This method allows the client to determine the parameters and / or requirements associated with the resource, or server capabilities, without implying a resource action or initiating a resource search.

Responses to this method are not cached.

If the OPTIONS request includes an entity body (as indicated by the presence of Content-Length or Transfer-Encoding), then the media type MUST be specified in the Content-Type field. Although this specification does not define any use for such a body, future HTTP extensions may use the OPTIONS body to create more detailed requests on the server. A server that does not support such an extension MAY refuse the request body.

If the Request-URI is an asterisk ("*"), the OPTIONS request is intended to be applied to the server as a whole, and not to a specific resource. Since the parameters of communication with the server are usually resource-dependent, the request "*" is only useful as the "ping" or "no-op" method; it does nothing, except that the client can check the capabilities of the server. For example, this can be used to check proxies for HTTP / 1.1 compliance (or lack thereof).

If the Request-URI is not an asterisk, the OPTIONS request applies only to the parameters available when communicating with this resource.

The answer for 200 SHOULD include any header fields that indicate additional functions implemented by the server and applicable to this resource (for example, Allow), possibly including extensions not defined by this specification. The response authority, if any, MUST also include information on communication options. The format for such an authority is not defined by this specification, but may be defined by future HTTP extensions. Content negotiation MAY be used to select the appropriate response format. If the response body is not included, the response MUST contain a Content-Length field with a value field of "0".

The Max Forward request header field MAY be used to target a specific proxy server in the request chain. When a proxy receives an OPTIONS request on an absolute URI that is allowed to forward the request, the proxy MUST check the Max-Forwards field. If the value of the Max-Forwards field is zero ("0"), the proxy MUST NOT forward the message; instead, the proxy MUST respond with its own communication parameters. If the value of the Max-Forwards field is an integer greater than zero, the proxy MUST decrease the value of the field when it forwards the request. If the request does not have a Max-Forwards field, then the forwarded request MUST NOT include the Max-Forwards field.

9.4 HEAD

The HEAD method is identical to GET, except that the server SHOULD NOT return the message body in response. The metadata contained in the HTTP headers in response to the HEAD request MUST be identical to the information sent in response to the GET request. This method can be used to obtain meta-information about the entity implied by the request, without transferring the object-object itself. This method is often used to check for hypertext links for authenticity, accessibility, and recent modification.

The response to the HEAD request MAY be cached in the sense that the information contained in the response MAY be used to update a previously cached object from this resource. If the new field values ​​indicate that the cached object is different from the current object (as indicated in the change in Content-Length, Content-MD5, ETag, or Last-Modified), then the cache MUST consider the cache entry obsolete.

+29
Jul 12 2018-11-11T00:
source share

OPTIONS tell you things like "What methods are allowed for this resource."

HEAD gets the HTTP header that you will get if you made a GET request, but without a body. This allows the client to determine the caching information, what type of content will be returned, which status code will be returned. Accessibility is only a small part of it.

+20
Jul 12 2018-11-11T00:
source share

OPTIONS method returns API information (methods / content type)

HEAD method returns resource information (version / length / type)

Server response

OPTIONS

 HTTP/1.1 200 OK Allow: GET,HEAD,POST,OPTIONS,TRACE Content-Type: text/html; charset=UTF-8 Date: Wed, 08 May 2013 10:24:43 GMT Content-Length: 0 

HEAD

 HTTP/1.1 200 OK Accept-Ranges: bytes Content-Type: text/html; charset=UTF-8 Date: Wed, 08 May 2013 10:12:29 GMT ETag: "780602-4f6-4db31b2978ec0" Last-Modified: Thu, 25 Apr 2013 16:13:23 GMT Content-Length: 1270 
  • OPTIONS Defining HTTP methods supported by the resource, for example. can we DELETE it or update it via PUT?
  • HEAD Checks if the resource has changed. This is useful when saving a cached version of a resource.
  • HEAD Retrieving metadata about a resource, for example. its media type or its size before making an expensive search possible
  • HEAD, OPTIONS Checks the availability and availability of the resource. For example, checking user links in an application

Here's a nice and concise article on how HEAD and OPTIONS fit into a RESTful architecture.

+4
Dec 01 '17 at
source share



All Articles