Download a Jira image with nodejs using authorization

Asked

Viewed 27 times

-1

Good afternoon, I need to download a profile image of Jira via REST. I can, but jira sends me an image referencing that I don’t have permission to see it (example of how the image is downloaded: image)

const express = require('express');
const cors = require('cors')
const request = require('request')
const app = express()
const download = require('download')

app.use(cors())

function callApiUserPhoto(callback){
    const options = {
        method: 'GET',
        url: `url/rest/api/2/user/search?username=user` , 
        auth: { username: "user", password: "senha" },
        headers: {
            'Accept': 'application/json'
        }
    };

    request.get(options, function(error, response, body) {
        if(response.statusCode != 200){
            return callback('error');
        }else{
            download(JSON.parse(body)[0].avatarUrls['48x48'], 'dist')
            return callback(JSON.parse(body)[0].avatarUrls['48x48'])
        } 
    })
}

app.get('/', (req, res) => {
    callApiUserPhoto(function (response) {
      if (response == 'error') {
        res.send("ops")
      } else {
        res.send(response)
      }
    })
  })

app.listen(4000, console.log("Sever online!"))

1 answer

0


I found a solution! I passed an authorized request (without the url) inside Else

const options2 = {
      method: 'GET',
      auth: { username: user, password: senha },
      headers: {
        'Accept': 'application/json'
      }
    };
      request(JSON.parse(body)[0].avatarUrls['48x48'], options2).pipe(fs.createWriteStream(`${user}.png`));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.