penggunaan contoh penggunaan user agent pada library node-fetch dalam bahasa javascript
Berikut adalah contoh penggunaan user agent pada library node-fetch dalam bahasa javascript:
const fetch = require('node-fetch');
const url = 'https://example.com';
const options = {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}
};
fetch(url, options)
.then(response => {
if (response.ok) {
return response.text();
}
throw new Error('Network response was not ok.');
})
.then(data => {
console.log(data);
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});
Pada contoh di atas, kita menggunakan node-fetch untuk melakukan request ke URL https://example.com dengan header User-Agent yang kita tentukan. Kemudian, kita memeriksa apakah responsenya OK atau tidak, dan jika OK, kita mencetak data responsenya ke konsol. Jika terjadi kesalahan selama proses fetch, kita mencetak pesan kesalahan ke konsol
原文地址: http://www.cveoy.top/t/topic/hozu 著作权归作者所有。请勿转载和采集!