ios - 使用 Axios 将本机反应到 Django [错误:请求失败,状态码 400]
问题描述
我已经尝试了几乎所有选项,但没有通过。我的 Django 服务器与 Postman 一起工作正常,但是当我从我的 React Native 应用程序中尝试它时,它给出了一个错误
[Error: Request failed with status code 400]
我的代码是:
submitData = () => {
console.log("submitQuestionDataToServer");
let form_data = new FormData();
form_data.append('qOne_img', Platform.OS === 'android' ? photoUri : photoUri.replace('file://', ''));
form_data.append('qOne_img', Platform.OS === 'android' ? secondPhotoUri : secondPhotoUri.replace('file://', ''));
form_data.append('question_title', qTitle);
form_data.append('asked_by', "http://127.0.0.1:8000/auth/users/2/");
let url = 'http://127.0.0.1:8000/api/questions/';
axios.post(url, form_data, {
headers: {
"Content-Type": "multipart/form-data",
"Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1Nk",
}
})
.then(res => {
console.log("res *******")
console.log(res.data);
})
.catch(err => console.log(err))
}
提前致谢。
解决方案
因为您正在向图像字段发送一个字符串。该值应该是blob类型。
它应该是这样的:
const filePath = Platform.OS === 'android' ? photoUri : photoUri.replace('file://', '')
form_data.append('qOne_img',{uri: filePath, type: mimeType, name: fileName} );
不确定 uri 是否在本机反应中工作。我rn-fetch-blob
用来处理 blob 的东西。
https://github.com/joltup/rn-fetch-blob#multipartform-data-example-post-form-data-with-file-and-data
推荐阅读
- tailwind-css - 如何在没有换行符的情况下在左侧制作带有图标的按钮?
- python - 如何在 Django 中获取 Queryset 的值
- c++ - 进程之间只有一个静态变量还是每个都有自己的?
- python - 如何使用 Tkinter 从 python 中的另一个文件访问函数。我找不到任何解决我的具体问题的方法
- javascript - 为什么我在 Firefox 控制台中得到两次输出?
- python - 匹配所有内容的正则表达式,直到找到一个单词
- reactjs - 导出地图组件时如何解决打字稿错误
- database - 图像名称未保存在数据库 codeigniter 3 中
- control-m - Control-M: SMART 文件夹 - 获取内部工作的信息
- react-native - 如何使用来自 react native 的令牌存储响应令牌和注销