pentf
    Preparing search index...

    Function fetch

    • fetch a URL. Apart from the first parameter, this implements the Fetch API. (Using this method rather than another enables outputting of curl commands with -c and a couple of defaults suitable for pentf).

      Parameters

      • config: any

        The pentf configuration object.

      • url: string

        URL to fetch.

      • Optionalinit: RequestInit & {
            agent?: any;
            cookieJar?: any;
            curl_extra_options?: any;
            curl_include_headers?: boolean;
        }

        fetch options, see RequestInit in the Fetch Spec. On top of the standard Fetch parameters, we support the following nonstandard parameters:

        • agent: node HTTP/HTTPS agent
        • curl_include_headers: boolean (default false) of whether to include -k in the curl output.
        • curl_extra_options: List of extra options for the curl output.
        • cookieJar: A CookieJar object to use. Pass in the string 'create' to create a new one (returned as response.cookieJar). The response will have a utility function async getCookieValue(name) to quickly retrieve a cookie value from the jar.
        • preferNativeFetch: use native node.js fetch instead of node-fetch. (experimental, doesn't work with self-signed certificates)
        • timeout: timeout in ms

      Returns any

      const response = await fetch(config, 'https://example.org/json-api', {
      method: 'POST',
      headers: {
      'Content-Type': 'application/json',
      },
      body: JSON.stringify({
      key: 'value',
      }),
      });
      assert.strictEqual(response.status, 200);
      const data = await response.json();