Search code examples
javascriptiframehttp-headersmeanjs

How to change the client informations in the request header (User-Agent)?


I am new to the world of web development using MEAN-JS, and I am trying to create multiple iframes to show the same website for different web browsers (to see the difference between ie7 and 9 for example), so I v tried to edit my User-Agent string but this doesn’t work.


Solution

  • Yes it is possible on the server side (the E,N in MEAN-JS), and easier using the request module, you just have to specify the user agent string in the option object inside a generic route to intercept any request from your website and test if it is a request for an image (bit file) or a plain text.
    You can find the user agent string that you want in this link:User Agent String.Com
    Like this:

    app.get('*', function (req, res){
            req.headers["user-agent"] = ‘YOUR USER AGENT STRING’;
            var options = {
                url: req.path,
                headers: {'user-agent':req.headers['user-agent'],
                            'accept':req.headers.accept,
                }
            };
            if ( /image\//.test(req.headers.accept)){
                options.encoding= null;}
            request(options, function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    res.set(
                response.headers
    
                );
                    res.send(body);
    
                }
                else {
                    console.log('request error: ' + JSON.stringify(error));
                    res.redirect('/');
                }
            });
        });