I have a simple HTTP request with javascript that uses the await
feature, however, I get the following result:
{} undefined
When I run the function, I have tested the headers and url with python and it works successfully with requests
am I missing something here?
import pkg from 'superagent';
const { get } = pkg;
const header = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Safari/605.1.15'
};
const url = 'https://books.toscrape.com';
class Agent {
constructor(url, headers){
this.url = url;
this.headers = headers;
}
getAgent = async () => {
const res = await get(this.url)
.set(this.headers);
return res.body;
}
};
const request = new Agent(url, header);
console.log(await request.getAgent());
I.e. this works successfully in python:
import requests
header = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Safari/605.1.15'
};
url = 'https://books.toscrape.com';
print(requests.get(url, header).content)