request上传返回错误码401

@ohos.request (上传下载),用这个下载文件正常,上传文件一直报401错误。日志内容:Failed to request the upload. err: {“code”:401}。代码如下:

let uploadConfig: request.UploadConfig = { 
  url: 'https://xxx-h5.huawei.com:xxx/xxx-portal-mobile/xxx/upload', 
  header: { 'Accept': '*/*','Authorization': MyInfoManager.getInstance().getToken() }, 
  method: "POST", 
  files: [{ filename: "file.jpg", name: "file", uri: 'file://media/Photo/3/IMG_1716256625_001/IMG_20240521_095525.jpg', type: "jpg" }], 
  data: [{ name: "name", value: "value" }], 
}; 
try { 
  let context = getContext(this) as common.UIAbilityContext 
  request.uploadFile(context, uploadConfig).then((data: request.UploadTask) => { 
 
  }).catch((err: BusinessError) => { 
    console.error(`Failed to request the upload. Code: ${err.code}, message: ${err.message}`); 
  }); 
} catch (err) { 
  console.error(`Failed to request the upload. err: ${JSON.stringify(err)}`); 
}
HarmonyOS
2024-11-08 09:37:25
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

在request上传时遇到错误码401,这通常意味着请求头参数不正确。建议检查下请求头参数,下方代码为标准的request上传文件示例代码可供参考:

import request, { UploadRequestOptions } from '@system.request'; 
 
let uploadRequestOptions: UploadRequestOptions = { 
  url: 'http://www.huawei.com', 
  method: 'POST', 
  files: [{ 
    filename: "test", 
    name: "test", 
    uri: "internal://cache/test.jpg", 
    type: "jpg" 
  }], 
  data: [{ 
    name: "name123", 
    value: "123" 
  }], 
  success: (data: object) => { 
    console.info(' upload success, code:' + JSON.stringify(data)); 
  }, 
  fail: (data:string, code:number) => { 
    console.info(' upload fail data: ' + data + 'code: ' + code); 
  }, 
  complete: () => { 
    console.info(' upload complete'); 
  } 
} 
 
try { 
  request.upload(uploadRequestOptions); 
  console.info('upload start '); 
} catch (err) { 
  console.info(' upload err:' + err); 
}
分享
微博
QQ
微信
回复
2024-11-08 16:15:06
相关问题
请求广告返回错误码401
2212浏览 • 1回复 待解决
请求广告返回错误码21800003
2104浏览 • 1回复 待解决
调用广告接口,返回错误码21800003
326浏览 • 1回复 待解决
startAbility跳转失败返回错误码16000001
1943浏览 • 1回复 待解决
华为事件通知接口返回10008错误码
9105浏览 • 2回复 待解决
TCPSocket on('close')错误码
312浏览 • 1回复 待解决
network bindSocket错误码问题
238浏览 • 1回复 待解决
错误码62980096怎么处理
811浏览 • 1回复 待解决
webview错误码105 是什么错误
703浏览 • 1回复 待解决
eglSwapBuffers常见错误码分析
403浏览 • 1回复 待解决
Web的onErrorReceive错误码含义
1654浏览 • 1回复 待解决
Socket连接报错,错误码88
1682浏览 • 1回复 待解决
OH_NativeImage_UpdateSurfaceImage接口错误码
1517浏览 • 1回复 待解决
startAbility跳转不成功,错误码16000001
1960浏览 • 1回复 待解决
错误码15500000(首选项)如何处理?
1165浏览 • 1回复 待解决
提问
该提问已有0人参与 ,帮助了0人