0

Get Always Use HTTPS setting

Published Nov 16, 2023

Reply to all requests for URLs that use "http" with a 301 redirect to the equivalent "https" URL. If you only want to redirect for a subset of requests, consider creating an "Always use HTTPS" page rule.

Script cloudflare
  • Submitted by hugo697 Typescript (fetch-only)
    Created 577 days ago
    1
    type Cloudflare = {
    2
      token: string;
    3
      email: string;
    4
      key: string;
    5
    };
    6
    /**
    7
     * Get Always Use HTTPS setting
    8
     * Reply to all requests for URLs that use "http" with a 301 redirect to the equivalent "https" URL. If you only want to redirect for a subset of requests, consider creating an "Always use HTTPS" page rule.
    9
     */
    10
    export async function main(auth: Cloudflare, zone_identifier: string) {
    11
      const url = new URL(
    12
        `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/settings/always_use_https`
    13
      );
    14
    
    
    15
      const response = await fetch(url, {
    16
        method: "GET",
    17
        headers: {
    18
          "X-AUTH-EMAIL": auth.email,
    19
          "X-AUTH-KEY": auth.key,
    20
          Authorization: "Bearer " + auth.token,
    21
        },
    22
        body: undefined,
    23
      });
    24
      if (!response.ok) {
    25
        const text = await response.text();
    26
        throw new Error(`${response.status} ${text}`);
    27
      }
    28
      return await response.json();
    29
    }
    30