Cross domain Missing required request header error with node.js

12,407

I found a Solution, just added this "res.header("Access-Control-Allow-Headers", "x-requested-with, x-requested-by");" to the headers, removed the other ones and removed cors server of the URL. Now its working fine.

The working code:

//Active Match
router.get('/activematchbyid/:server/:sid', function(req, res, next) {
    var serverList = {br: 'BR1', na: 'NA1', eune: 'EUNE1', euw: 'EUW1', kr: 'KR1', lan: 'LAN1', las: 'LAS1', oce: 'OCE1', tr: 'TR1', ru: 'RU'};
    var url = 'https://' + req.params.server + '.api.pvp.net/observer-mode/rest/consumer/getSpectatorGameInfo/' + serverList[req.params.server] + '/' + req.params.sid + '?api_key=' + apiKey;
    res.header("Access-Control-Allow-Headers", "x-requested-with, x-requested-by");
    request(url, function(err, resp, body){
        String.prototype.beginsWith = function (string) {
            return(this.indexOf(string) === 0);
        }
        if (body.beginsWith('<html>') || body.beginsWith('Missing')){   
            res.send(resp.statusCode);  
        }else{
            body = JSON.parse(body);
            res.send(body);            
        };        
    });
});
Share:
12,407
Murilo Bastos
Author by

Murilo Bastos

Updated on June 04, 2022

Comments

  • Murilo Bastos
    Murilo Bastos almost 2 years

    I'm working with an API based on json requests, all the requests are workining but one. The problematic request does not allow cross-domain by default, but using 'Cors' it works. The problem is that when I'm testing the request with javascript using the cors server it works, but when I'm using it with node.js it doesnt.

    error: 'Missing required request header. Must specify one of: origin,x-requested-with'
    

    The code that does'nt work:

    //Active Match
    router.get('/activematchbyid/:server/:sid', function(req, res, next) {
        var serverList = {br: 'BR1', na: 'NA1', eune: 'EUNE1', euw: 'EUW1', kr: 'KR1', lan: 'LAN1', las: 'LAS1', oce: 'OCE1', tr: 'TR1', ru: 'RU'};
        var url = 'https://cors-anywhere.herokuapp.com/https://' + req.params.server + '.api.pvp.net/observer-mode/rest/consumer/getSpectatorGameInfo/' + serverList[req.params.server] + '/' + req.params.sid + '?api_key=' + apiKey;
        console.log(url);
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "X-Requested-With");
        request(url, function(err, resp, body){
            String.prototype.beginsWith = function (string) {
                return(this.indexOf(string) === 0);
            }
            if (body.beginsWith('<html>') || body.beginsWith('Missing')){   
                res.send('error', 200);  
            }else{
                console.log(body);
                body = JSON.parse(body);
                res.send(body);            
    
            };        
        });
    });
    

    This is the code that works:

    var serverList = {br: 'BR1', na: 'NA1', eune: 'EUNE1', euw: 'EUW1', kr: 'KR1', lan: 'LAN1', las: 'LAS1', oce: 'OCE1', tr: 'TR1', ru: 'RU'};
    $.ajax({
        url: 'https://cors-anywhere.herokuapp.com/https://' data.server + '.api.pvp.net/observer-mode/rest/consumer/getSpectatorGameInfo/' + serverList[data.server] + '/' + data.sid + '?api_key=' + apiKey;,
        success: function(result){
    
            //code stuff
    
        }
    });
    

    My website does the request to this url: http://gankei-backend.herokuapp.com/activematchbyid/parameters-here