⚡ Z-Fetch

HTTP Methods

Advanced HTTP Methods

OPTIONS, TRACE, HEAD, and CUSTOM methods for specialized use cases.

Beyond the basic HTTP methods, Z-Fetch provides support for advanced HTTP methods used in specialized scenarios.

Advanced Methods

  • OPTIONS - Check supported methods and capabilities
  • TRACE - Diagnostic tool for debugging requests
  • HEAD - Get response headers without body
  • CUSTOM - Use any custom HTTP method

Method Examples

OPTIONS Request

Use OPTIONS to check what methods and capabilities a server supports:

import { OPTIONS } from "@z-fetch/fetch";
 
const checkCapabilities = async () => {
  const { data, error, response } = await OPTIONS(
    "https://api.example.com/posts",
  );
 
  if (response) {
    console.log("Allowed methods:", response.headers.get("Allow"));
    console.log(
      "CORS headers:",
      response.headers.get("Access-Control-Allow-Headers"),
    );
  } else {
    console.error("Error:", error.message);
  }
};

TRACE Request

TRACE is useful for debugging request paths through proxies:

import { TRACE } from "@z-fetch/fetch";
 
const traceRequest = async () => {
  const { data, error } = await TRACE("https://api.example.com/posts");
 
  if (data) {
    console.log("Request trace:", data);
  } else {
    console.error("Error:", error.message);
  }
};

Note

Many servers disable TRACE for security reasons. Check server configuration if you encounter issues.

HEAD Request

Get response headers without downloading the full response body:

import { HEAD } from "@z-fetch/fetch";
 
const checkResourceInfo = async () => {
  const { response, error } = await HEAD(
    "https://api.example.com/large-file.zip",
  );
 
  if (response) {
    console.log("Content-Length:", response.headers.get("Content-Length"));
    console.log("Content-Type:", response.headers.get("Content-Type"));
    console.log("Last-Modified:", response.headers.get("Last-Modified"));
  } else {
    console.error("Error:", error.message);
  }
};

CUSTOM Request

Use any custom HTTP method for specialized protocols:

import { CUSTOM } from "@z-fetch/fetch";
 
const customMethodRequest = async () => {
  // Example with a custom method like CONNECT or proprietary methods
  const { data, error } = await CUSTOM(
    "https://api.example.com/custom-endpoint",
    "CUSTOM",
    {
      body: {
        customData: "example",
      },
    },
  );
 
  if (data) {
    console.log("Custom response:", data);
  } else {
    console.error("Error:", error.message);
  }
};

Real-World Example: WebDAV

import { CUSTOM } from "@z-fetch/fetch";
 
// WebDAV PROPFIND method
const webdavPropfind = async () => {
  const { data, error } = await CUSTOM(
    "https://webdav.example.com/folder/",
    "PROPFIND",
    {
      headers: {
        Depth: "1",
        "Content-Type": "application/xml",
      },
      body: `<?xml version="1.0" encoding="utf-8" ?>
        <D:propfind xmlns:D="DAV:">
          <D:prop>
            <D:displayname/>
            <D:getcontentlength/>
            <D:getlastmodified/>
          </D:prop>
        </D:propfind>`,
    },
  );
 
  if (data) {
    console.log("WebDAV properties:", data);
  } else {
    console.error("WebDAV error:", error.message);
  }
};

Use Cases

OPTIONS:

  • CORS preflight checks
  • API capability discovery
  • Server feature detection

TRACE:

  • Request debugging
  • Proxy troubleshooting
  • Security testing

HEAD:

  • File size checking
  • Cache validation
  • Resource existence checks

CUSTOM:

  • WebDAV operations (PROPFIND, MKCOL, MOVE, COPY)
  • Custom API protocols
  • Legacy system integration

On this page