javascript - 更改 axios header 中的 Content-Type 以修复 415 错误

标签 javascript rest http reactjs axios

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

这是我目前的错误:

cherrypy._cperror.HTTPError: (415, 'Expected an entity of content type application/json, text/javascript')

根据我的阅读,我需要更改我的发布请求中的 Content-Type,我环顾四周我目前正在尝试这样做:

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; boundary=----WebKitFormBoundaryBwjjjGuJEySeXdRU

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

最佳答案

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',
        }
    }
    )
}

关于javascript - 更改 axios header 中的 Content-Type 以修复 415 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41469119/

相关文章:

javascript - 我应该用什么来用 JS 创建 2D 游戏(跑酷游戏)?

javascript - RegExp 连续检测到多个单字母实例?

apache - 如何使用 Apache Nifi 查询 REST API?

json - 具有多个唯一 ID 的 Rest API 设计

http - azure Blob : "The condition specified using HTTP conditional header(s) is not met"

javascript - 将公共(public)变量传递给 Node.js 中的模块

javascript - 错误消息不清楚 :[$injector:unpr]

java - 使用子字段版本使用 Spring Data 进行乐观锁定

linux - 文件大小超过 60K 时文件上传失败

c# - 如何在 Startup.cs ASP.NET Core 中删除 WebDav