B
B
bozuriciyu2019-12-13 15:09:32
Node.js
bozuriciyu, 2019-12-13 15:09:32

How to put header in http.get?

I read https://nodejs.org/api/http.html#http_http_get_opt...
I did not understand anything and everything I tried does not work.
Here is my code

const options = url.parse(imgUrl);
      let client;
      if (options.protocol === 'https:') {
        client = https;
      } else if (options.protocol === 'http:') {
        client = http;
      } else {
        throw new Error('The string should have "http" or "https" scheme.');
      }

const req = client.get(options, function(response) {
       ...
}

How to pass headers to the first argument? If I make an object with url, then an error.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Antsiferov, 2019-12-13
@Babayka_od

client.get({path: 'https://...', headers: {'x-header': 'header value'}}, (res) => {})

There is a description of options here - https://nodejs.org/api/http.html#http_http_request...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question