因为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()
}
}
当请求内容为空时,不建议设置空字符串,会有异常,可以不设置content内容或者将content内容定义为 undefined或空对象{}试一下
示例
req.content = undefined;
或者
req.content = {};