向Redmine API发送POST请求时如何解决"422无法处理的实体"? [英] How to Fix '422 Unprocessable Entity' when sending a POST request to Redmine API?

查看:362
本文介绍了向Redmine API发送POST请求时如何解决"422无法处理的实体"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a wiki page using redmine rest api. The Authentication was succeeded, however the wiki page is not being created because of a 422 error.

Redmine文档说:当尝试使用无效或缺少属性参数的对象创建或更新对象时,您将收到422 Unprocessable Entity响应.这意味着无法创建或更新对象."

The Redmine documentation says: "When trying to create or update an object with invalid or missing attribute parameters, you will get a 422 Unprocessable Entity response. That means that the object could not be created or updated."

但是我似乎可以找出我的困境.当我提出第二个请求"PUT REQUEST"时问题就解决了.

But I can seem to find out where I have mess up. The PROBLEM CAME UP WHEN I DID THE SECOND REQUEST- "PUT REQUEST".

所以我们知道问题出在那部分.

so we know the problem is somewhere in that section.

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

My guess is, it is either the file path or the content-type.

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

This is what I have so far....

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错误.

I am suppose to see a wiki page being created in my redmine dashboard but I am getting a 422 error.

推荐答案

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

You are sending the update request to the JSON api, i.e. <redmine_url>/projects/Testing/wiki/WikiTesting.json with Content-Type: application/octet-stream. Because of this, Redmine is unable to parse the PUTed payload since it doesn't know in what format the data is.

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

To solve this, you should always make sure to set the correct content type when posting data. In this case, you should set the Content-Type header to application/json when sending any JSON-formatted data to Redmine.

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

Note that in principal, you can send XML data to Redmine and get JSON back. The output format is determined by the file ending in the URL (.json or .xml), the format of the data sent by you is always identified by the Content-Type header.

这篇关于向Redmine API发送POST请求时如何解决"422无法处理的实体"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆