I want to donwload a file via http and check the "ContentType" response header. My Download looks like this:
var fileUrl = "<url>";
var request = https.get(fileUrl, function (res) {
res.on('data', function (data) {
//...
});
res.on('error', function (error) {
//...;
});
I get the data, but is there any way to acces the content type resonse header?
To get HTTP headers with Node. js, we can use the res. headers properties. const http = require("http"); const options = { method: "HEAD", host: "example.com", port: 80, path: "/", }; const req = http.
response. writeHead(200) sends a response header to the request. The status code is a 3-digit HTTP status code, like 404. This method must only be called once on a message and it must be called before response. end() is called.
The http. createServer() method turns your computer into an HTTP server. The http. createServer() method creates an HTTP Server object. The HTTP Server object can listen to ports on your computer and execute a function, a requestListener, each time a request is made.
The res
variable is an instance of http.IncomingMessage
, which has a headers
property that contains the headers:
var request = https.get(fileUrl, function (res) {
var contentType = res.headers['content-type'];
...
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With