javascript - “string”参数必须是 string、Buffer 或 ArrayBuffer 类型之一。接收到的类型对象
问题描述
在给定的代码中出现此错误。我在文件上传时调用此方法,它将捕获块。
ERR: TypeError [ERR_INVALID_ARG_TYPE]: The "string" argument must be one of type string, Buffer, or ArrayBuffer. Received type object
try{
request
.post(url)
.set(postHeaders)
.send(postData)
.end(function (err, response) {
if(err) {
console.log(err);
res.send(errorJson);
return;
}
res.set(response.header);
res.send(response.text);
});
} catch(err){
console.log("error" , err)
}
标题是
'content-type': 'multipart/form-data;'
解决方案
从外观上看,您正在使用它superagent
来执行上传。由于您打算进行multipart/form-data
上传,因此不能使用.send()
. 从文档:
多部分请求 SuperAgent 也非常适合构建它提供方法 .attach() 和 .field() 的多部分请求。
当您使用 .field() 或 .attach() 时,您不能使用 .send() 并且不能设置 Content-Type (将为您设置正确的类型)
因此,您需要将代码更改为:
request
.post(url)
.field(<setYourFieldDataHere>) // or use .attach() if you want to upload files
//
推荐阅读
- http - API 总是错误 408 - 发送的数据类型无效
- php - Preg_match 正在截断数组中的最后一个元素
- python - Pandas:如何根据另一列中的元素数量增加一列中的元素?
- flutter - 使用 GN 构建系统,如何将 cflag 设置为目标中的特定源文件
- php - 运行 2 个不同 php 版本的 Symfony 项目
- python - 未找到 Pandas groupby 列
- sql - 将多个表与不常见的列组合在一起
- android - 设置从蓝牙发送和接收数据之间的延迟
- scala - 未来 onComplete 每隔一段时间工作一次
- bash - 在while循环中打印当前迭代