简体   繁体   中英

NodeJS + Request - Access denied when requesting website

I'm trying to request the html of a website using request but I keep getting an access denied error. How do I get past this? Here is the code for the function below:

const request = require('request');
function firstShoe() {
        request('https://www.jdsports.co.uk/product/green-nike-vapormax/281735/', function (error, response, body) {
            console.log('body:', body); 
        });
}

Error:

</BODY>
</HTML>

body: <HTML><HEAD>
<TITLE>Access Denied</TITLE>
</HEAD><BODY>
<H1>Access Denied</H1>

You don't have permission to access "http&#58;&#47;&#47;www&#46;jdsports&#46;co&#46;uk&#47;product&#47;green&#45;nike&#45;vapormax&#47;281735&#47;" on this server.<P>
Reference&#32;&#35;18&#46;609d3e17&#46;1500116386&#46;15f0cb85
</BODY>
</HTML>

Found a solution by passing the user-agent into the headers.

function firstShoe() {
        var options = {
            headers: {'user-agent': 'node.js'}
        }
        request('https://www.jdsports.co.uk/product/green-nike-vapormax/281735/', options, function (error, response, body) {
            console.log(body);
            message.channel.send(body);
        });
    }

You are getting a 403 Forbidden because that website is blocking all requests sent using non common user agents (basically they check User-Agent header). It is a very simple protection to avoid scrappers.

For example, if you send the following cURL using its standard User-Agent, the response is received perfectly:

curl -v 'https://www.jdsports.co.uk/product/green-nike-vapormax/281735/'

Nevertheless, if you repeat that request specifying a non existing User-Agent, the request is blocked:

curl -v 'https://www.jdsports.co.uk/product/green-nike-vapormax/281735/' -H 'User-Agent: StackOverflow'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM