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()
}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
HarmonyOS
2024-12-27 18:26:21
999浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

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

示例

req.content = undefined;

或者

req.content = {};

分享
微博
QQ
微信
回复
2024-12-27 20:16:43


相关问题
HarmonyOS RCP GET请求POST请求如何传参
1016浏览 • 1回复 待解决
使用rcp模块能力发送Get请求
2029浏览 • 1回复 待解决
HarmonyOS rcp如何进行fetch流式请求
780浏览 • 1回复 待解决
rcp模块能力发起post请求
2197浏览 • 1回复 待解决
RCP库发起请求如何设置超时时间
1255浏览 • 1回复 待解决
HarmonyOS rcp网络请求返回数据null
944浏览 • 1回复 待解决
Harmonyos js fetch请求失败
9651浏览 • 2回复 待解决