1
const URL = 'https://www.imdb.com/title/tt0816222/? 
ref_ = fn_al_tt_2 ';

(async() => {

    const response = await request({
      uri: URL,
      headers: {
        'Connection': 'keep-alive',
        'User-Agent': 'Mozilla/0.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'

      },

    });

I need help from this code. How can I get the response header values in console of visual studio code for the following site.

8
  • Show us what you have tried? Commented Feb 6, 2019 at 8:29
  • Please add all code to the question itself, not to the comment section Commented Feb 6, 2019 at 8:56
  • if my answer was useful, please mark as correct. Commented Feb 6, 2019 at 9:17
  • accept the answer if found the correct solution. Commented Feb 6, 2019 at 11:24
  • @Ariz that's what I meant actually...I miss the word at the time :D Commented Feb 6, 2019 at 11:50

5 Answers 5

1

Just handle the Promise from request library

  request({
    uri: 'https://www.imdb.com/title/tt0816222/?',
    headers: /*your headers*/ 
    })
    .then(function(response){
       console.log(response.headers)
    })
Sign up to request clarification or add additional context in comments.

4 Comments

That's the same of async/await
@CristianTraìna I just look at his comment where it's written require('request-promise') and assume that async/await is not supported, that's why I've posted the Promise approach.
@ElmerDantas why async await is not supported with request-promise? it returns a promise only.
@Ariz I've said "I assume" that is not supported. Just a guess. The only reason for posting a Promise approach.
0

You will get the response headers in response.headers

4 Comments

But It says undefined?
console.log(response.headers); Or console.log(response.headers['user-agent']); tried both but nothing works
I think the issue is that request module doesn't return a promise.It accepts a callback.So you can't use async await syntax in this case
so what to do? Is there any method?
0

Print like this

console.log(response.headers)

1 Comment

const request = require('request-promise'); const cheerio = require('cheerio'); const URL = 'imdb.com/title/tt0816222/?ref_=fn_al_tt_2'; (async () => { const response = await request({ uri:URL, headers:{ 'Connection': 'keep-alive', 'User-Agent': 'Mozilla/0.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36' }, }); console.log(response.headers); })()
0

This code prints headers:

const URL = 'https://www.imdb.com/title/tt0816222/?ref_ = fn_al_tt_2';
const request = require('request');

(async () => {

  const response = await request({
    uri: URL,
    headers: {
      'Connection': 'keep-alive',
      'User-Agent': 'Mozilla/0.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'

    },

  });
  console.log(response.headers);
})();

Comments

0

Because you are just fetching the body of response from request npm.

add resolveWithFullResponse: true in request options.

const URL = 'https://www.imdb.com/title/tt0816222/? 
ref_ = fn_al_tt_2 ';

(async() => {

    const response = await request({
      uri: URL,
      headers: {
        'Connection': 'keep-alive',
        'User-Agent': 'Mozilla/0.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'

      },
	resolveWithFullResponse: true
    });

if you need to only headers

const URL = 'https://www.imdb.com/title/tt0816222/? 
ref_ = fn_al_tt_2 ';

(async() => {

    const {headers} = await request({
      uri: URL,
      headers: {
        'Connection': 'keep-alive',
        'User-Agent': 'Mozilla/0.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'

      },
	resolveWithFullResponse: true
    });
    
    console.log(headers)

2 Comments

thanks brother it worked now reverse it how can we get the request headers instead of response and how can we get only one specific header value and store it in variable?
now response.header is just a native object so you simply can do response.header['content-type'] etc, here you will get the value of content-type header. updated answer if you need only header.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.