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该如何处理?

阅读 488
1 个回答

可以尝试将:

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

换成:

context.request.headers.osVersion = bundleData["versionName"];
logo
HarmonyOS
子站问答
访问
宣传栏