Search code examples
javascripthttp-headersaxioshttp-status-code-401redmine-api

Getting 401 error when using redmine API for a POST request even though I have included the api key


I am trying to make a post request to create a new wiki page using the redmine-api. I am using JavaScript and Axios. However I a getting a 401 error(UnAuthorize).

My goal is to be able to send a word document to my redmine and create a wiki page.

I am using the Api key provided and I did enable the rest api feature in my redmine setting

I have included the api key in the header however it is not working.

  var wordDocument = "./Redmine.docx"

   axios.post('<website url>/uploads.json', {
    headers: {
        'Content-Type': 'application/octet-stream',
        'Cache-Control': 'no-store',
        'key': '<api-key>'
    },
    data:wordDocument

    })
    .then(function (response) { 
       console.log("succeeed--->  "); 
       console.log    (response) 
     })
    .catch(function (error) {
        console.log("failed----->  ");
        console.log(error.response.headers)
        console.log(error.message)
        console.log("failed----->  ");
    })


I am getting a status: '401 Unauthorized',


Solution

  • Alright I got it working. I did "axios({})" instead of "axios.post". I do not know what the different is? I thought it was the same. Here is my code for anyone who run into this.\

    var wordDocument = "./Redmine.docx"
    axios({
        method: 'post',
        url: '<redmind_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)
        })
    .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----->  ");
    })