更改 axios 标头中的 Content-Type 以修复 415 错误

新手上路,请多包涵

我正在尝试通过 axios post 请求将文件发送到我的后端。

这是我目前的错误:

cherrypy._cperror.HTTPError: (415, ‘预期内容类型为 application/json, text/javascript 的实体’)

根据我所阅读的内容,我需要在我的帖子请求中更改内容类型,我环顾四周我目前正在尝试这样做:

 handleUploadButton(e){
            const upload_file = this.state.file;
            const formData = new FormData();
            formData.append('file', upload_file);
            const request = axios.post(someUrl, formData, {headers: {
                "Content-Type": "application/json"}
            })
                .then(function (response) {
                    console.log('successfully uploaded', upload_file);
                });
    }

不确定是否相关,但所有这些都是通过 reactjs 表单发生的。这是我当前的内容类型:Content-Type:multipart/form-data;边界=—-WebKitFormBoundaryBwjjjGuJEySeXdRU

我不知道从这里去哪里。任何帮助将不胜感激。

原文由 theJuls 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 330
1 个回答
SignIn = () => {
    console.log('login clicked')
    let data = JSON.stringify({
        password: this.state.password,
        username: this.state.email
    })

    axios.post('url', data, {
        headers: {
            'Content-Type': 'application/json',
        }
    }
    )
}

原文由 Ben Ahlander 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题