HarmonyOS RCP get请求时,在拦截器中增加header参数报错

class HttpHeaderInterceptor implements rcp.Interceptor {
  async intercept(context: rcp.RequestContext, next: rcp.RequestHandler): Promise<rcp.Response> {
    const url = context.request.url.toString();

    logger.debug("拦截 url:" + context.request.url.toString());

    let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION |
    bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_METADATA;
    let bundleData = await bundleManager.getBundleInfoForSelf(bundleFlags)
    context.request.headers!["osVersion"] = bundleData["versionName"];
    context.request.headers!["deviceid"] = deviceinfo.ODID;
    context.request.headers!["deviceModel"] = deviceinfo.distributionOSVersion;
    context.request.headers!["appName"] = "qx_harmony";


    logger.debug("最终请求:" + context.request.url.toString());
    const promise = next.handle(context);
    promise.then((resp) => {
      resp.statusCode;
    });
    return promise;
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

在拦截器中增加header参数get请求报错,检查context.request.headers为空post可以。如果要get请求在拦截中增加header该如何处理?

HarmonyOS
2024-12-23 17:21:12
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

可以尝试将:

context.request.headers!["osVersion"] = bundleData["versionName"];
  • 1.

换成:

context.request.headers.osVersion = bundleData["versionName"];
  • 1.
分享
微博
QQ
微信
回复
2024-12-23 20:50:59
相关问题
HarmonyOS rcp请求拦截器
1499浏览 • 1回复 待解决
HarmonyOS rcp拦截器
754浏览 • 1回复 待解决
HTTP GET请求如何传递参数
4132浏览 • 1回复 待解决
HarmonyOS router有没有拦截器?
567浏览 • 1回复 待解决
HarmonyOS RCP GET请求、POST请求如何传参
1038浏览 • 1回复 待解决
使用rcp模块能力发送Get请求
2052浏览 • 1回复 待解决
HarmonyOS 是否有前置路由拦截器
604浏览 • 1回复 待解决
HarmonyOS 导航路由拦截器如何实现
683浏览 • 1回复 待解决