首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当向Redmine API发送POST请求时,如何修复“422无法处理的实体”?

当向Redmine API发送POST请求时,如何修复“422无法处理的实体”?
EN

Stack Overflow用户
提问于 2019-06-22 07:39:55
回答 2查看 31.6K关注 0票数 2

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

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

但我似乎能找出我哪里搞砸了。当我执行第二个请求-- "PUT请求“时,问题出现了。

所以我们知道问题就在这个部分的某个地方。

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

这就是我到目前为止所拥有的……

代码语言:javascript
运行
复制
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----->  ");
        })

}

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-25 00:32:25

您正在将更新请求发送到JSON api,即使用Content-Type: application/octet-stream<redmine_url>/projects/Testing/wiki/WikiTesting.json。因此,Redmine无法解析PUTed有效负载,因为它不知道数据的格式。

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

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

票数 5
EN

Stack Overflow用户

发布于 2020-05-10 15:08:47

当我从flutter应用程序上传多个文件到服务器时,我遇到了类似的问题;问题是一些服务器需要有[]格式才能接收多个文件;

代码语言:javascript
运行
复制
 => 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)),
  ));

在这里,我只是将密钥从视频更改为videos[]

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56711503

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档