File upload 如何修复';422个不可加工实体';向Redmine API发送POST请求时?

File upload 如何修复';422个不可加工实体';向Redmine API发送POST请求时?,file-upload,axios,redmine,redmine-api,http-status-code-422,File Upload,Axios,Redmine,Redmine Api,Http Status Code 422,我正在尝试使用redmine rest api创建wiki页面。 身份验证已成功,但由于422错误,未创建wiki页面 Redmine文档说:“当试图创建或更新具有无效或缺少属性参数的对象时,您将得到422个不可处理的实体响应。这意味着无法创建或更新该对象。” 但我似乎能找到我把事情搞砸的地方。当我执行第二个请求时,问题出现了——“PUT请求” 所以我们知道问题就在那一部分 我的猜测是,它要么是文件路径,要么是内容类型 这就是我目前所拥有的 const wordDocument="C:\User

我正在尝试使用redmine rest api创建wiki页面。 身份验证已成功,但由于422错误,未创建wiki页面

Redmine文档说:“当试图创建或更新具有无效或缺少属性参数的对象时,您将得到422个不可处理的实体响应。这意味着无法创建或更新该对象。”

但我似乎能找到我把事情搞砸的地方。当我执行第二个请求时,问题出现了——“PUT请求”

所以我们知道问题就在那一部分

我的猜测是,它要么是文件路径,要么是内容类型

这就是我目前所拥有的

const wordDocument="C:\Users\adasani\Desktop\practice\RedmineApi/RedmineText.txt";

creatingWikiPage_Request(wordDocument);

function creatingWikiPage_Request(wordDocument) {

    axios({
        method: 'post',
        url: '<redmine_url>/uploads.json',
        headers: { 'Content-Type': 'application/octet-stream' },
        params: { 'key': '<api-key>' },
        data: wordDocument
    })
        .then(function (response) {
            console.log("succeeed--->  ");
            console.log(response.data.upload.token)
            axios({
                method: 'put',
                url: '<redmine_url>/projects/Testing/wiki/WikiTesting.json',
                headers: { 'Content-Type': 'application/octet-stream' },
                params: { 'key': '<api-key>' },
                data: {

                    "wiki_page": {
                        "text": "This is a wiki page with images, and other files.",
                        "uploads":[ 
                            { "token": response.data.upload.token, "filename": "RedmineText.txt", "content-type": "text/plain" }
                        ]
                    }

                }

            })
                .then(response => {
                    console.log("PUT is Succeed-->>>")
                    console.log(response)
                })
                .catch(error => {
                    console.log("Error-->>")
                    console.log(error.response)
                })

        })
        .catch(function (error) {
            console.log("failed----->  ");
            console.log(error.response.statusText, "-->", error.response.status);
            console.log(error.response.headers)
            console.log(error.message)
            console.log("failed----->  ");
        })

}


const wordDocument=“C:\Users\adasani\Desktop\practice\RedmineApi/RedmineText.txt”;
创建WikiPage_请求(wordDocument);
函数创建WikiPage_请求(wordDocument){
axios({
方法:“post”,
url:“/uploads.json”,
标题:{'Content Type':'application/octet stream'},
参数:{'key':''},
资料:wordDocument
})
.然后(功能(响应){
console.log(“succeed-->”);
日志(response.data.upload.token)
axios({
方法:'放',
url:“/projects/Testing/wiki/WikiTesting.json”,
标题:{'Content Type':'application/octet stream'},
参数:{'key':''},
数据:{
“维基页面”:{
“文本”:“这是一个包含图像和其他文件的wiki页面。”,
“上载”:[
{“token”:response.data.upload.token,“文件名”:“RedmineText.txt”,“内容类型”:“text/plain”}
]
}
}
})
。然后(响应=>{
log(“放置成功-->>”)
console.log(响应)
})
.catch(错误=>{
console.log(“错误-->>”)
console.log(error.response)
})
})
.catch(函数(错误){
console.log(“失败------>”;
日志(error.response.statusText,“-->”,error.response.status);
console.log(error.response.headers)
console.log(错误消息)
console.log(“失败------>”;
})
}

我假设看到在我的redmine仪表板中创建了一个wiki页面,但我收到了一个422错误。

您正在向JSON api发送更新请求,即
/projects/Testing/wiki/WikiTesting.JSON
带有
内容类型:application/octet stream
。因此,Redmine无法解析计算的有效负载,因为它不知道数据的格式

要解决此问题,您应该始终确保在发布数据时设置正确的内容类型。在这种情况下,向Redmine发送任何json格式的数据时,应将
内容类型
标题设置为
application/json


注意,原则上,您可以将XML数据发送到Redmine并获取JSON。输出格式由URL(
.json
.xml
)结尾的文件决定,您发送的数据格式始终由
内容类型
标题标识。

我在从我的Flatter应用程序向服务器上载多个文件时遇到类似问题;问题是某些服务器需要[]格式才能接收多个文件

 => Change From
formData.files.add(MapEntry(
    "videos",
    await MultipartFile.fromFile(curPost.url, filename: getFileNameByFullPath(curPost.url)),
  ));


=> TO
formData.files.add(MapEntry(
    "videos[]",
    await MultipartFile.fromFile(curPost.url, filename: getFileNameByFullPath(curPost.url)),
  ));

在这里,我只需将键从视频更改为视频[]

是的,这就是错误所在,谢谢。我一小时前才注意到。