Search code examples
node.jshttpshttp-headershttp-status-code-403node-https

Node.js https fails, whereas browser, `curl` & `wget` succeed?


Succeeding requests:

$ url='https://svn.tools.ietf.org/svn/tools/xml2rfc/trunk/cli/xml2rfc/data/xml2rfc.css'
$ curl "$url"
$ wget -qO - "$url"
$ python -c 'import webbrowser; webbrowser.open("'"$url"'", new=2)'

Failing request:

$ echo 'require("https").get("'"$url"'", res => console.info("statusCode:", res.statusCode, ";"));' | node

Output of failing request:

statusCode: 403 ;

Solution

  • I assume you are trying to get a file from a remote server which you do not have access. You have to set user agent when you make a call to a remote server with nodejs https package. Try this code:

    let https = require('https')
    let pageUrl = 'https://svn.tools.ietf.org/svn/tools/xml2rfc/trunk/cli/xml2rfc/data/xml2rfc.css'
    let url = require('url')
    
    let options = url.parse(pageUrl)
    options.headers = {
        'User-Agent': 'request'
    };
    let req = https.get(options, (res) => {
        console.log("statusCode: ", res.statusCode);
        console.log("headers: ", res.headers);
        let data = ""
        res.on('data', (chunk) => {
            data += chunk
        })
    
        res.on('end', (chunk) => {
            console.log("ended")
            console.log(data)
        })
    })