[英]How to get value of response headers in nodejs
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.如何在以下站点的 Visual Studio 代码控制台中获取响应标头值。
Just handle the Promise from request
library只需处理request
库中的 Promise
request({
uri: 'https://www.imdb.com/title/tt0816222/?',
headers: /*your headers*/
})
.then(function(response){
console.log(response.headers)
})
您将在 response.headers 中获得响应标头
像这样打印
console.log(response.headers)
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);
})();
Because you are just fetching the body of response from request npm.因为您只是从请求 npm 中获取响应正文。
add resolveWithFullResponse: true in request options.在请求选项中添加 resolveWithFullResponse: true 。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.