HarmonyOS 使用fetch方式发送请求,在POST方式时rcp.Request如何设置请求内容为空?

因为header会变化,所有使用fetch方式发送请求,在POST方式时rcp.Request如何设置请求内容为空

private async req(url: string, method: string = 'POST', params?: Record<string, string>): Promise<object> {
  try {
  let req = new rcp.Request(url);
  req.content = "" // 当参为空,且还是想用POST请求时,content应该如何传递?
  if (params) {
    let arr = Object.keys(params)
    if (arr.length > 0) {
      req.content = new rcp.Form(params)
    }
  }
  req.method = method.toUpperCase()
  req.headers = this.header()
  req.configuration = this.requestConfiguration()
  let resp: rcp.Response = await QDHttpRequest.session.fetch(req)
  return resp
} catch (err) {
  QDLogUtils.error(`req err = ${JSON.stringify(err)}`)
  return new Object()
}
}
阅读 514
1 个回答

当请求内容为空时,不建议设置空字符串,会有异常,可以不设置content内容或者将content内容定义为 undefined或空对象{}试一下

示例

req.content = undefined;

或者

req.content = {};

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进