How to filter specific html div and get literal html as result using both Cheerio and Request? - xmlhttprequest

I managed to get entire html from the URL using codes below but I only need a single div with class of "product-overview" and save it to database.
const request = require('request');
const cheerio = require("cheerio");
request('https://www.aliexpress.com/item/1005002976829559.html', (error, response, html) => {
if (!error && response.statusCode == 200) {
const $ = cheerio.load(html);
const description = $('.product-overview');
// how to filter <div class="product-overview"> .. </div>
console.log(description.html());
}
});
Current response is : null

Related

XMLHttpRequest interception

I want that when some XMLHttpRequest at 'https://myurl.com' be send, isntantly returns status code 200 and myOwnResponse at response body, I don't want that the request go to the server, the reason is that the server response is very slow, and I want to increase the performance.
I want something like tweak: mock and modify HTTP requests, how can I do that?
var _open = XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function (method, URL) {
const myUrl = 'https://myurl.com'
if(method === 'GET' && URL === myUrl) {
const myOwnResponse = {'myResponse': true}
return myOwnResponse
} else {
return _open.apply(this, arguments);
}
};

How to query out params in URL - React Native

I am trying to extract the code from an incoming url in react native. I am able to receive the url and print it in my console but having trouble getting the param from url.
My code:
useEffect(() => {
Linking.addEventListener('url', callback);
})
callback = async (url) => {
if (url !== null) {
const new_url = JSON.stringify(url)
console.log(new_url)
const urlCallback = new URL(new_url);
const code = urlCallback.searchParams.get('code');
console.log(code)
}
};
I keep getting this error: TypeError: Invalid URL: {"url":"myapp://?code=ABCDEFG"}
I am trying to query out that code so i can send it as a param in a post request.
Appreciate any help, thanks!
Based on the error message, it seems you're stringifying url object, not string. Try to parse from its 'url' prop instead.
callback = async (url) => {
if (url !== null) {
const new_url = url.url;
console.log(new_url)
const urlCallback = new URL(new_url);
const code = urlCallback.searchParams.get('code');
console.log(code)
}
};
Try this query-string
const parsed = queryString.parseUrl("myapp://?code=ABCDEFG");
console.log(parsed.query.code)

How to make a api call using axios to a cgi file

I'm using axios to make a call to an API that I created, I'm passing two values email and password.
My api always retrn empty because my cgi file is not getting anything for email & password from axios or
postmate(using to test the api call), but if in the browser I load the full url it works fine.
Any ideas what am I doing wrong.
Part of the form
initialValues = {{email: '', password:''}}
onSubmit = {(values, {setSubmitting}) => {
if (values.email == '' || values.password == '') {
handleMessage("Please fill all the fieldss");
setSubmitting(false);
} else {
handleMessage("");
setSubmitting(true);
handleLogin(values, setSubmitting);
}
}}
AXIOS CALL
const handleLogin = (data, setSubmitting) => {
const url = 'https://mydesitte.or/register.cgi';
axios
.post(url, data)
.then((response) => {
// const result = response.data;
// const {message, status, data} = result;
alert(data.email);
setSubmitting(false);
}).catch(error => {
alert(error);
setSubmitting(false);
});
}
CGI file
my $cgi = new CGI;
# This is always empty when I do the call from the my react native app or postmate
my $email = $cgi->param('email');
my $password = $cgi->param('password');
If I go to this url on the browser https://mysite.or/register.cgi?email=email&password=pass work fine
You need to pass POST data as an object:
axios.post(url, {
email: email,
password: pass
})

Testcafe multiple requests to endpoint synchronously not working

Currently I am setting up testcafe and have hit a hurdle when making a request to the same endpoint.
This is an authentication request. The steps are currently:
Initial request is sent to server which responds with a body. This is awaited
Once step is retrieved from step 1 this is mutated and sent back to the server.
We then the same endpoint and pass in the body generated in step 2
But I am having issues in the testcafe tests as it always using the first request and returns that twice.
I have referred to this issue on testcafe https://github.com/DevExpress/testcafe/issues/2477 however this did not work.
const mock = (mockResponse, status) => {
return RequestMock()
.onRequestTo(/\/apitest\/authenticate\)
.respond(mockResponse, status, {
"Access-Control-Allow-Origin": "http://localhost:3000",
"Access-Control-Allow-Credentials": "true"
});
};
const mock1 = mock(RESPONSE1, 200);
const mock2 = mock(RESPONSE2, 200);
test.requestHooks(mock1)(
"Should redirect to error if user has been suspended after submitting",
async t => {
const usernameInput = Selector("#username");
const mobileDigitOne = Selector("#mobile-digit-0");
const mobileDigitTwo = Selector("#mobile-digit-1");
const mobileDigitThree = Selector("#mobile-digit-2");
const submitButton = Selector("#submit-button");
await t
.typeText(usernameInput, "username123456")
.click(digits)
.typeText(digits, "123456")
.click(submitButton);
await t.removeRequestHooks(mock1);
await t.addRequestHooks(mock2);
Any ideas on how to request same endpoint multiple times?
You can distinguish different requests to the same endpoint using the request body. You can pass different kinds of filter to the mock's onRequestTo method, including a predicate function. Refer to the Filter with a Predicate article for more details.
Thus, you can use a single mock for both requests, like this:
function isTheSecondRequest(requestBody) {
// This should return `true` for the mutated request body
// and `false` for the original one.
//
// For example:
return requestBody.indexOf('{ foo: bar }') !== -1;
}
const mock = RequstMock()
.onRequestTo(request => {
request.url === '/apitest/authenticate' &&
!isTheSecondRequest(request.body)
})
.respond(RESPONSE1, 200, HEADERS)
.onRequestTo(request => {
request.url === '/apitest/authenticate' &&
isTheSecondRequest(request.body)
})
.respond(RESPONSE2, 200, HEADERS);

External API Call Using Express

I'm trying to call a College Score Card API using Express and Request. When I search for a specific school, I receive results from several schools, but not the school I searched for. Here is part of my code:
var fields = '_fields=school.name,2013.aid.median_debt.completers.overall,2013.repayment.1_yr_repayment.completers,2013.earnings.10_yrs_after_entry.working_not_enrolled.mean_earnings&page=100';
var requestUrl = 'https://api.data.gov/ed/collegescorecard/v1/schools.json?api_key=' + apiKey + '&' + fields;
module.exports = function(app) {
app.get('/school', function(req, res, next) {
request(requestUrl, function (error, response, body) {
if (!error && response.statusCode == 200) {
var json = JSON.parse(body);
console.log(json);
} else {
console.log("There was an error: ") + response.statusCode;
console.log(body);
}
});
})
};
HTML:
<form action="/school" method="GET">
<input type="text" class="form-control" name="school_name" value="" id="enter_text">
<button type="submit" class="btn btn-primary" id="text-enter- button">Submit</button>
</form>
You need to incorporate the school name into the URL. From your form that is set for method=GET, the name will come in req.query.school_name. So, instead of just sending the request to requestUrl, you send it to:
requestUrl + "&school_name=" + req.query.school_name
which will add this onto the end of the URL:
&school_name=Pepperdine
Or, put into your code, it would look like this:
module.exports = function(app) {
app.get('/school', function(req, res, next) {
request(requestUrl + "&school_name=" + req.query.school_name, function (error, response, body) {
if (!error && response.statusCode == 200) {
var json = JSON.parse(body);
console.log(json);
res.send(...); // send some response here
} else {
console.log("There was an error: ") + response.statusCode;
console.log(body);
res.send(...) // send some response here
}
});
})
};