Skip to content

fetch

工具栏 -> 服务 -> 添加服务 -> 网络 API 。

访问百度。

async function main() {
  const response = await HTTP.fetch('https://www.baidu.com/');
  const text = await response.text();
  console.log(text);
}

main();

访问 Cloudflare D1 数据库。

const TOKEN = '123456';
const ID = '1234';
const DATABASE_ID = 'ab12';
const sql = {sql: "SELECT k, v FROM kv"};

const url = `https://api.cloudflare.com/client/v4/accounts/${ID}/d1/database/${DATABASE_ID}/query`;
const options = {
  method: 'POST',
  headers: { 'Authorization': 'Bearer ' + TOKEN },
  body: JSON.stringify(sql)
};

async function main() {
  const response = await HTTP.fetch(url, options);
  const data = await response.json();
  console.log(data.result[0].results);
}

main();

联系 math@baima.site