如何为上传请求生成Boundary?

const httpRequest = http.createHttp() 
const formData = new FormData() 
 
for (const key in body) { 
  formData.append(key, body[key]); 
} 
 
const fileName = filepath.substring(filepath.lastIndexOf('/') + 1); 
formData.append('file', filepath, fileName) 
try { 
  const response = await httpRequest.request(url, { 
    method: http.RequestMethod.POST, 
    header: headers, 
    extraData: body 
  }) 
 
  if (response.responseCode === http.ResponseCode.OK){ 
    return response.result 
  }else { 
    return Promise.reject({code: response.responseCode, message: response.responseCode + ""}) 
  } 
} catch (e) { 
  return Promise.reject({code: "-1", message: "unknown error"}) 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

以上的请求由于Content-Type为multipart/form-data,后端报错org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found请问如何生成合适的boundary?

HarmonyOS
2024-09-10 12:39:18
1908浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

当Header中,'content-Type’为’multipart/form-data’时生效,需要使用multiFormDataList上传数据,自API 11开始支持该属性 https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-network-kit/js-apis-http.md

分享
微博
QQ
微信
回复
2024-09-10 15:50:17


相关问题
NEXT网络请求如何上传大文件 ?
2764浏览 • 1回复 待解决
HarmonyOS 上传下载如何获取请求响应
928浏览 • 1回复 待解决
如何为 C++ 提供回调函数?
3116浏览 • 1回复 待解决
如何为网页设置单个Cookie的值。
897浏览 • 1回复 待解决
java代码如何为button添加xml背景?
4554浏览 • 2回复 待解决
HarmonyOS 如何上传文件?
622浏览 • 1回复 待解决
HarmonyOS 如何生成UUID?
799浏览 • 1回复 待解决