CF Workers反向代理并修改请求

用于访问被墙的api请求

async function handleRequest(event) {
    const request = event.request
    const host = "https://example.com/Channel";

    let new_request_headers = new Headers(request.headers);
    new_request_headers.set("Content-Type", "application/x-www-form-urlencoded")
    new_request_headers.set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36")
    new_request_headers.set("Accept", "*/*")
    new_request_headers.delete("Authorization")
    const data = JSON.parse(await request.text())
    const modifiedRequest = new Request(host, {
        body: "value="+encodeURIComponent(data['value']),
        headers: new_request_headers,
        method: "POST",
        redirect: request.redirect
    })
    return await fetch(host, modifiedRequest);
}

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

 Reference:

  https://qastack.cn/programming/35325370/post-a-x-www-form-urlencoded-request-from-react-native

  https://developers.cloudflare.com/workers//runtime-apis/request

posted @ 2022-08-10 16:35  Naihe\  阅读(582)  评论(0编辑  收藏  举报
// 音乐播放器