Dracks
Dracks

Reputation: 107

Express, Get img from api and send this img

I am looking to get an image from an API and send it back to mine without saving this image.

whatever i do the picture is broken once what's going through my api.

my code :

    app.get('/student/getImg/:login', (req, res) => {
    //ajout gestion size ?size=medium
    if(req.session.cookietna) {
        var options = 
        { 
            method: 'GET',
            url: "https://auth.etna-alternance.net/api/users/"+req.params.login+"/photo", 
            headers: 
            {
                Cookie: req.session.cookietna
            }
        };
        request(options)
        .then(function (response) {
            res.writeHead(200, {
                'content-type': 'image/jpeg'
            });
             res.end(response);
        })
        .catch(function (err) {
            res.status(400).jsonp({ error: err });
        });
    } else
    res.status(401).jsonp({ error: "you'r not log, please go on /login" });
});

Original API header : here
thank you in advance for your help and advice !

Upvotes: 0

Views: 90

Answers (1)

Rahul Sharma
Rahul Sharma

Reputation: 10071

You can directly stream image in response.

var url = 'https://www.planwallpaper.com/static/images/6768666-1080p-wallpapers.jpg';
return request.get(url).pipe(res);

Upvotes: 1

Related Questions