PaulChess / tinyimg-cli

帮你自动完成图片压缩的cli
MIT License
0 stars 0 forks source link

请求tinypng.com的请求头 #2

Open PaulChess opened 3 years ago

PaulChess commented 3 years ago
  buildRequestParams() {
      return {
          method: 'POST',
          hostname: 'tinypng.com',
          path: '/web/shrink',
          headers: {
              rejectUnauthorized: false,
              'X-Forwarded-For': this.getRandomIP(),
              'Cache-Control': 'no-cache',
              'Content-Type': 'application/x-www-form-urlencoded',
              'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 '
                  + '(KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
          }
      };
  }
PaulChess commented 3 years ago
  1. 发起请求后,会得到以下信息:

    {
    input: { size: 62182, type: 'image/jpeg' },
    output: {
    size: 20213,
    type: 'image/jpeg',
    width: 260,
    height: 260,
    ratio: 0.3251,
    url: 'https://tinypng.com/web/output/86h65gc7t8w20vfm6b7u5r1dv12j41b0'
    }
    }
  2. 使用new URL()对上述返回的url进行包装,可得到如下信息:

    URL {
    href: 'https://tinypng.com/web/output/86h65gc7t8w20vfm6b7u5r1dv12j41b0',
    origin: 'https://tinypng.com',
    protocol: 'https:',
    username: '',
    password: '',
    host: 'tinypng.com',
    hostname: 'tinypng.com',
    port: '',
    pathname: '/web/output/86h65gc7t8w20vfm6b7u5r1dv12j41b0',
    search: '',
    searchParams: URLSearchParams {},
    hash: ''
    }