【问题标题】:trouble to sending the bearer token with fetch and axios使用 fetch 和 axios 发送不记名令牌的麻烦
【发布时间】:2021-12-09 11:22:02
【问题描述】:

我在尝试的两个替代方案中遇到了令牌问题

uploadAttachment: async (file, id) => {
    const token = await AsyncStorage.getItem('token');
    let params = { sale: id };

    let formData = new FormData();
    formData.append('file', file, file.name);
    try {
      const req = await fetch(`${BASE_API}/sales/upload-attachment`, formData, {
        method: 'POST',
        headers: {
          Authorization: token,
          Accept: 'application/json',
          'Content-Type': 'multipart/form-data',
        },

      });

      /* console.log('que que ta dando' + JSON.stringify(req)); */

      //const json = await req.json();
      //return json;
    } catch (err) {
      console.log(err);
      return err.error;
    }
  },



uploadAttachment: async (file, id) => {
  const token = await AsyncStorage.getItem("token");
  let params = { sale: id };

  let data = new FormData();
  data.append("file", file, file.name);

  try {
    let result = await axios.post(`${BASE_API}/sales/upload-attachment`, {
      data,
      headers: {
        Authorization: `${token}`,
        Accept: "application/json",
      },
    });
    console.log(result);
  } catch (error) {
    console.error(error.response);
  }
};

这两个代码无法将令牌传递给 api,这个 api 工作正常,因为 web 部分没问题,但在移动设备上我遇到了问题

【问题讨论】:

    标签: axios fetch expo multipartform-data bearer-token


    【解决方案1】:

    以这种方式更改格式,

    fetch

    uploadAttachment: async (file, id) => {
        const token = await AsyncStorage.getItem('token');
        let params = { sale: id };
    
        let formData = new FormData();
        formData.append('file', file);//here should be file object
        try {
           const req = await fetch(`${BASE_API}/sales/upload-attachment`, {
                 method: 'POST',
                 headers: {
                   Authorization: token,
                   'Accept': 'application/json',
                   'Content-Type': 'multipart/form-data',
                 },
                 body : formData
           });
        } catch (err) {
            console.log(err);
            return err.error;
        }
    
    
    }
    

    axios

    uploadAttachment: async (file, id) => {
        const token = await AsyncStorage.getItem('token');
        let params = { sale: id };
    
        let formData = new FormData();
        formData.append('file', file);//here should be file object
        try {
           const req = await axios.post(`${BASE_API}/sales/upload-attachment`, formData, {
                 headers: {
                   Authorization: token,
                   "Content-Type" : "multipart/form-data",
                   'Accept': 'application/json',
                 }
           });
        } catch (err) {
            console.log(err);
            return err.error;
        }
    
    
    }
    

    【讨论】:

    • 问题不在这两种情况下,而是在 react native 版本中,上传通过 post 传递的文件在 react native 的最新版本中存在问题,我使用的替代方法是将 expo sdk 恢复为40
    猜你喜欢
    • 2017-04-20
    • 1970-01-01
    • 2018-07-19
    • 2023-03-28
    • 2019-04-07
    • 2020-03-19
    • 2020-11-20
    • 1970-01-01
    • 2021-12-26
    相关资源
    最近更新 更多