axios 在react native app中从客户端向服务器发送视频时,我收到错误400

yyyllmsg  于 2023-04-20  发布在  iOS
关注(0)|答案(1)|浏览(110)

错误:[AxiosError:请求失败,状态代码为400]我在虚拟机或物理机上运行了react native应用程序,它给出了错误。我在ip localhost上运行应用程序
使用Axios和FormData的客户端

const App = () => {
  const baseUrl = "http://192.168.3.222:5000/"
  const [singleFile, setSingleFile] = useState();

  const uploadVideo = async () => {
    if (singleFile != null) {

      console.log({ singleFile })
      console.log('Uri: ', singleFile.uri)
      const formdata = new FormData();

      formdata.append('video', singleFile);

      console.log(formdata);

      axios({
        method: "POST",
        url: `${baseUrl}uploadvideo`,
        headers: {
          'Content-Type': 'multipart/form-data; ',
        },
        data: formdata,
      })
        .then(res => console.log(res.data))
        .catch(err => console.log("err: ", err));

    } else {

      alert('Please Select File first');
    }
  };

const selectFile = async () => {

    try {

      console.log("btn selectfile click")
      const res = await DocumentPicker.pickSingle({

        type: [DocumentPicker.types.allFiles],
        presentationStyle: 'fullScreen',
        copyTo: 'cachesDirectory'
      });

      setSingleFile(res);

    } catch (err) {
      setSingleFile(null);
      if (DocumentPicker.isCancel(err)) {

        alert('Canceled');
      } else {

        alert('Unknown Error: ' + JSON.stringify(err));
        throw err;
      }
    }
  };

在Python中使用Flask的服务器

@app.route('/uploadvideo', methods=['POST'])
def upload2():
    video = request.files['video']
    video.save('data/training_data/video/' + video.filename)
    return video.filename

当我从 Postman 发送请求时,没有错误发生。

7vux5j2d

7vux5j2d1#

我认为你应该将请求头的'Content-Type'改为'application/json'

相关问题