javascriptnode.jsfetch-apihttp-options-method

How to send ‘OPTIONS * HTTP/1.1’ requests with the fetch() JavaScript API?


From RFC 9110, HTTP Semantics:

An OPTIONS request with an asterisk ("*") as the request target (Section 7.1) applies to the server in general rather than to a specific resource. Since a server's communication options typically depend on the resource, the "*" request is only useful as a "ping" or "no-op" type of method; it does nothing beyond allowing the client to test the capabilities of the server. For example, this can be used to test a proxy for HTTP/1.1 conformance (or lack thereof).

I wrote an HTTP server in JavaScript running on Node and would like to write a unit test for checking how it responds to the following request:

OPTIONS * HTTP/1.1

How to send that request using the fetch() JavaScript API?

I know that it is possible with Curl using the --request-target option (the question was asked here on Stack Overflow):

curl --request-target '*' -X OPTIONS https://example.com/

Solution

  • It is not possible to issue HTTP request with OPTIONS * HTTP/1.1 start line in JavaScript

    I have scourged all documentations of both fetch and XMLHttpRequest and found no way to change the "request target resource" part of the HTTP request start line ("prologue").

    All JS APIs derive content of the start line from the method and URL, while the resource is always the "path" part of the URL and defaults to "/".

    There is no way to tell JS to issue request with "*" in place where the default / normally occurs.

    On top of that, there does not seem to be any other low-level alternative that would let us construct whole HTTP request payload including start line "by hand", so there are likely no workarounds possible.


    Posting as clear answer for current states of things and documentations. Would be happy to be proven wrong.