如何在访存API中修改请求对象的网址?

问题描述 投票:0回答:1

我正在使用由HTTP请求触发的Cloudflare worker。我想接收传入的请求,更改URL,但保持所有其他属性不变,然后发出请求。结构如下:

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {

  #change the value of request.url and then...

  return fetch(request)
    .then(response => {
        return response;
    });
}
javascript http fetch httprequest httpresponse
1个回答
0
投票

您可以这样做:

async function handleRequest(request) {

  #change the value of request.url and then...

  return fetch({...request, url: "someUrl"})
    .then(response => {
        return response;
    });
}

这将用“ someUrl”覆盖网址希望这会有所帮助:)

© www.soinside.com 2019 - 2024. All rights reserved.