estk
V2EX  ›  Serverless

Cloudflare Workers 返回 request method、headers、body,用于开发自测请求信息

By estk at 2022 年 7 月 3 日 · 4297 次点击
addEventListener("fetch", event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  let req = '<table>\n'
  req += `<tr>\n  <td>method</td>\n  <td>${request.method}</td>\n</tr>\n`
  request.headers.forEach((value, key)=>{
    req += `<tr>\n  <td>${key}</td>\n  <td>${value}</td>\n</tr>\n`
  })
  req += `<tr>\n  <td>body</td>\n  <td>${await request.text()}</td>\n</tr>\n`
  req += '</table>'
  return new Response(req, {
    headers: {
      'content-type': 'text/html'
    }
  })
}

1 条回复
patrickyoung
   1
patrickyoung  
   2022 年 7 月 3 日 via iPhone
https://echo.paw.cloud/
© 2026 V2EX · 29ms · 3.9.8.5