How to Fix '422 Unprocessable Entity' when sending a POST request to Redmine API?

不想你离开。 提交于 2021-02-10 05:58:26

问题


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.

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."

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----->  ");
        })

}


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


回答1:


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.

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.

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.




回答2:


I have similar issue while uploading multiple files to server from my flutter app; The issue is some server needs to have [] format to receive multiple files;

 => 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)),
  ));

Here I just make change key from videos to videos[].



来源:https://stackoverflow.com/questions/56711503/how-to-fix-422-unprocessable-entity-when-sending-a-post-request-to-redmine-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!