gpt4 book ai didi

request - 如何在服务 worker 中接收请求有效负载?

转载 作者:行者123 更新时间:2023-12-03 20:59:04 24 4
gpt4 key购买 nike

我使用 service worker 拦截对安全资源的 HTTP 请求并向请求添加授权 header (如果用户已经登录)。现在,我有一个场景,服务 worker 拦截一个 POST 请求以注入(inject)授权 header 。但是,服务 worker 没有接收请求有效负载中的数据,因此没有请求有效负载(参见屏幕截图 2)。因此,nodejs 中的后端验证逻辑失败,因为没有接收到有效负载数据。理想情况下,除了授权 header 之外,后端代码还应该接收有效负载数据以保存在数据库中。有什么指导吗?

下面的第一个屏幕截图是带有有效负载的原始请求。第二个到服务 worker 的请求没有任何请求有效负载。

这是我拦截获取请求的服务 worker 代码:

self.addEventListener('fetch', (event) => {
const fetchEvent = event;
const requestProcessor = (idToken) => {
console.log('idToken in fetch '+idToken);
let req = event.request;
// For same origin https requests, append idToken to header.
if (self.location.origin == getOriginFromUrl(event.request.url) &&
(self.location.protocol == 'https:' ||
self.location.hostname == 'localhost') &&
idToken) {
// Clone headers as request headers are immutable.
const headers = new Headers();
for (let entry of req.headers.entries()) {
headers.append(entry[0], entry[1]);
}
// Add ID token to header. We can't add to Authentication header as it
// will break HTTP basic authentication.
headers.append('Authorization', 'Bearer ' + idToken);
try {
req = new Request(req.url, {
method: req.method,
headers: headers,
mode: 'same-origin',
credentials: req.credentials,
cache: req.cache,
redirect: req.redirect,
referrer: req.referrer,
body: req.body,
bodyUsed: req.bodyUsed,
context: req.context
});
} catch (e) {
console.error('failed to prepare new header '+ e);
// This will fail for CORS requests. We just continue with the
// fetch caching logic below and do not pass the ID token.
}
}

return fetch(req);

};
// Try to fetch the resource first after checking for the ID token.
// getIdToken() returns valid idtoken for logged in user.
event.respondWith(getIdToken().then(requestProcessor, requestProcessor));
});

  • Original request before service worker execution

  • Service worker processed request

    问候,
    桑托什

    最佳答案

    这实际上非常简单,假设有效负载是请求的主体。获取/访问它的具体方式取决于请求正文的内容类型:

    event.request.arrayBuffer() // for content-type of arrayBuffer
    event.request.blob() // for content-type of blob
    event.request.json() // for content-type of json
    event.request.text() // for content-type of text
    event.request.formData() // for content-type of formData
    每个人都返回一个 promise 。
    归功于 https://stackoverflow.com/a/39653321/1971662 .

    关于request - 如何在服务 worker 中接收请求有效负载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59298889/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com