Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the response "ContentType" in a nodejs http request

Tags:

node.js

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?

like image 899
Nathan Avatar asked May 10 '16 10:05

Nathan


People also ask

How do I read response headers in node JS?

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.

What is RES writeHead in node JS?

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.

What is HTTP createServer Nodejs?

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.


Video Answer


1 Answers

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'];
  ...
});
like image 161
robertklep Avatar answered Oct 22 '22 23:10

robertklep