Ad
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 ;
Ad
Answer
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)
})
})
Ad
source: stackoverflow.com
Related Questions
- → Maximum call stack exceeded when instantiating class inside of a module
- → Browserify api: how to pass advanced option to script
- → Node.js Passing object from server.js to external modules?
- → gulp-rename makes copies, but does not replace
- → requiring RX.js in node.js
- → Remove an ObjectId from an array of objectId
- → Can not connect to Redis
- → React: How to publish page on server using React-starter-kit
- → Express - better pattern for passing data between middleware functions
- → Can't get plotly + node.js to stream data coming through POST requests
- → IsGenerator implementation
- → Async/Await not waiting
- → (Socket.io on nodejs) Updating div with mysql data stops without showing error
Ad