0

I am making a get request to a web site and want to get content-length from response headers.

import rp from "request-promise";

const options = {
  uri: "http://www.dadaabstories.org/",
  resolveWithFullResponse: true
};

rp(options).then(res => {
  console.log(res.headers);
}).catch(err => {
  throw err;
});

The response I am getting is without content-length:

server: 'openresty',
date: 'Tue, 05 Mar 2019',
'content-type': 'text/html; charset=UTF-8',
'transfer-encoding': 'chunked',
connection: 'close',
vary: 'Accept-Encoding, X-UA-Device, Accept, Accept-Encoding',
'x-rid': 'adbfe4b6b62c7a71bce234e33a6f07f5',
p3p: someData',
'x-xss-protection': '1; mode=block',
'x-content-type-options': 'someData',
'x-tumblr-user': 'someData',
'x-tumblr-pixel-0': 'someData',
'x-tumblr-pixel-2': 'someData',
'x-tumblr-pixel': 'someData',
link: 'someData',
'x-robots-tag': 'noindex, nofollow',
'x-ua-compatible': 'someData',
'x-ua-device': 'someDevice'

But for this demo web site http://stack.imgur.com/ I get content-length.

I've learned from this that 'transfer-encoding': 'chunked' is disabling content-length. So how actually can I get it?

Like how Chrome browser is showing the size. Maybe even it's not contetn-length response that I am trying to get.

Chrome dev tool

undefinedUser
  • 217
  • 1
  • 2
  • 10

1 Answers1

0

This is a manner of the backend of the website. If the server by default does not include the Content-Length, that it is usually included during the exchange of file, generally there is no possibility in the HTTP protocol to explicitly ask the header that you want.

Fucio
  • 475
  • 3
  • 11
  • The resource that you followed explains how to include the content lenght in the response of a call from the server side. In this case you are not providing a response to a HTTP call but fetching a resource online. Depending on the website you fetch the content length might be included or not. – Fucio Mar 05 '19 at 09:11
  • Would it be correct if I count it like so? `Buffer.byteLength(JSON.stringify(res);` – undefinedUser Mar 05 '19 at 10:29
  • If you are fetching via REST API yes, but generally you have to be sure to retrieve a JSON objects, otherwise don't try to cast it in a object. – Fucio Mar 05 '19 at 13:14