使用IDE:
DevEco Studio NEXT Developer Beta1
Build Version: 5.0.3.403, built on June 20, 2024
编程环境:
API 12 Beta1
省流
Header头设置方式为 header: {'Content-Type': ''} ,用于覆盖 harmonyOS 默认的Header参数'Content-Type':'application/json';
let uploadConfig: request.UploadConfig = {
url: url,
header: {'Content-Type': ''},
method: http.RequestMethod.PUT,
files: files,
data:[]
}
详细的分析历程
根据官方文档进行上传文件调试:
返回的报文和错误信息如下:
07-15 09:02:16.866 I upload headerReceive taskState: {"headers":{"connection":"keep-alive","content-length":"169","content-type":"text/html","date":"Mon, 15 Jul 2024 01:02:16 GMT","etag":"\"63186cb7-a9\"","server":"nginx"},"body":"<!DOCTYPE html>\n<html>\n<head>\n <title>403 Forbidden</title>\n <meta charset=\"utf-8\">\n</head>\n<body>\n <p>这里什么都没有,放弃吧。</p>\n</body>\n</html>\n"}
07-15 09:02:16.869 I [js_notify_data_listener.cpp OnNotifyDataReceive 190] OnNotifyDataReceive type is progress, tid is 687328788
07-15 09:02:16.869 I upload progress taskState: 6762 - 6762
07-15 09:02:16.876 I [js_notify_data_listener.cpp OnNotifyDataReceive 190] OnNotifyDataReceive type is failed, tid is 687328788
07-15 09:02:16.877 I upload fail taskState: {"path":"/data/storage/el2/base/haps/entry/cache/20240708_upload.jpg","responseCode":17,"message":"Http protocol error"}
查询API:
header receive 打印出来的错误码是403,就是参数的问题;
查询到gitee的官方参考文档:
就是这个上传会默认把Content-Type设置为application/json,需要覆盖它,让后台自己判定内容格式就好;有人可能就会写成以下的形式,但是并没有卵用;
// 上传任务配置项
let header = new Map<Object, string>();
header.set('Content-Type', '');
正确的覆盖格式是:
let uploadConfig: request.UploadConfig = {
url: url,
header: {'Content-Type': ''},
method: http.RequestMethod.PUT,
files: files,
data:[]
}
运行代码,解决问题;