Aaron Dasani
Aaron Dasani

Reputation: 73

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',

Upvotes: 0

Views: 1452

Answers (2)

Aaron Dasani
Aaron Dasani

Reputation: 73

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

Upvotes: 0

Raphael Rafatpanah
Raphael Rafatpanah

Reputation: 20007

Try using the other authentication methods mentioned in the docs:

x passed in as a "key" parameter
- passed in as a username with a random password via HTTP Basic authentication
- passed in as a "X-Redmine-API-Key" HTTP header (added in Redmine 1.1.0)

https://www.redmine.org/projects/redmine/wiki/Rest_api#Authentication

Also ensure that you're using the correct API key.

You can find your API key on your account page ( /my/account ) when logged in, on the right-hand pane of the default layout.

Upvotes: 0

Related Questions