file-uploadaxiosredmineredmine-apihttp-status-code-422

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


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.


Solution

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