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;
  }
}

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

阅读 487
1 个回答

可以尝试将:

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

换成:

context.request.headers.osVersion = bundleData["versionName"];
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏