How can I see the HTTP status code from the request made by page.open? - phantomjs

I have a phantomJS script that contains the following:
page.open(url, function (status) {
if (status === "fail") { /* handle failure */ }
});
The status check works sometimes, but the status will still be "success" even if the request returns 500. How can I get the actual request status code?

You can do it something like this:
var page = require('webpage').create(),
system = require('system'),
resources = [];
page.open('http://google.com', function (status) {
console.log('Loaded with http status:', resources[0].status);
phantom.exit();
});
page.onResourceReceived = function(response) {
// check if the resource is done downloading
if (response.stage !== "end") return;
// apply resource filter if needed:
if (response.headers.filter(function(header) {
if (header.name == 'Content-Type' && header.value.indexOf('text/html') == 0) {
return true;
}
return false;
}).length > 0)
resources.push(response);
};
So, if you need to check the status of the first browser's request (in this case google's html page) you should see it as the first one returned in resources[0].status. In onResourceReceived handler you can add more filters for resources you try to get http code from.
UPDATE: thanks to #fotijr, added a check for completed responses

In
page.property('onResourceError', function(res) {
resources variable is undefined,
even if I set it with
var page = require('webpage').create(),
system = require('system'),
resources = [];

Related

Nuxt JS route validation doesn't redirect to error page

I have an app with Nuxt JS, and there is a route called posts that accepts parameters like so: .../posts/_id. When someone goes to /posts/put_news, they get post with name "Put News" and so on.
So, I wrote a validation method like so:
async validate({ params }) {
// await operations
const response = await axios.get('http://localhost:5000/listings_names')
var response_data = response.data
var str = (params.id).split('_').join(' ')
const arr2 = str.split(" ");
for (var i = 0; i < arr2.length; i++) {
arr2[i] = arr2[i].charAt(0).toUpperCase() + arr2[i].slice(1);
}
const str2 = arr2.join(" ");
var id_fix = str2
const obj = response_data.find(o => o.name == id_fix);
console.log(obj)
if (obj == undefined){
console.log('undefied, false')
return false
}
else{
return true;
}
},
The code does return false, but does nothing else. Once it returns "false" I expect nuxt to redirect the user to the error page, but it just stays on that page. I looked on the documentation, and it seems like the user should be automatically redirected to an error page, however nothing happens here. Also, my nuxt version is 2.15.8.
Thank you for the help
I fixed the issue, so all I had to do, was add redirect to validate function, and redirect to the error page, like so:
async validate({ params, redirect }) { //run-functions
if (obj == undefined) {
redirect('/not_found')
return false
} else {
return true
}
}

How to check http status code in vuejs and return value

I need to check the status code in vuejs, whether is it 200 or else. Here is my code, but i have errors.
methods:{
userSearch(){
let searchUrl = dataUrl+this.usersearch;
fetch(searchUrl).then(statusCode => {
if(statusCode == 200){
message = "Not Available"
$("#availability").innerHTML = message
}
else {
message = "Available"
$("#availability").innerHTML = message
}})
this should return in my p element with id="availability" whether the user is available or not, depending on the status code. I am calling this method in the input field on enter.
As #deceze pointed out, the fetch function resolves to a Response Object.
You can see here how to properly handle the HTTP status of the response.
But, for your code, it should be something like this:
userSearch() {
let searchUrl = dataUrl+this.usersearch;
fetch(searchUrl).then(response => {
if(response.status == 200){
message = "Not Available"
$("#availability").innerHTML = message
}
else {
message = "Available"
$("#availability").innerHTML = message
}
})
}
As just part of the code was provided, there might be other causes for the error, that I cannot see with just this snippet.

Cloudflare Worker Breaks Redirects On Site

I have a cloudflare worker that will insert custom CSS into the page if the country is not U.S/Canada. This works perfectly, however - it will break all redirects when the CSS is inserted. Attached below is the worker scripts
addEventListener('fetch', event => {
event.passThroughOnException()
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const country = request.cf.country
if (country != 'US' && country !='CA') {
const response = await fetch(request)
const type = response.headers.get("Content-Type") || "";
if (!type.startsWith("text/html")) {
return response;
}
var html = await response.text()
// Inject scripts
const customScripts = 'styling here'
html = html.replace( /<\/body>/ , customScripts)
// return modified response
return new Response(html, {
headers: response.headers
})
}
}
The redirects are broken because they use a special HTTP status code (usually 301 or 302), but your Worker code is not copying the status code over to the final response, so the final response ends up always having a 200 status code.
Try changing this:
return new Response(html, {
headers: response.headers
})
To this:
return new Response(html, response)
This way, the new response copies over all of the properties of the old response, except for the body. This includes the status property, so the status code will be copied over.
By the way, unrelated to this problem, I notice another issue with your code:
if (country != 'US' && country !='CA') {
It looks like if this condition evaluates to false (that is, if country is either 'US' or 'CA'), then your handleRequest() function doesn't return a response at all. In this case, an exception will be thrown, and the client will see an 1101 error page. I recommend adding an else clause that returns the response that you want users in the US and Canada to see.

Cloudflare worker redirections

Lets say that I have a website asdf.com, and I would like to write a worker that will do a lot of redirections (more than 30). For example www.asdf.com/app -> app-asdf.com, www.asdf.com/dashboard -> dashboard-asdf.com.
I tried with following, but it doesn't work:
async function handleEvent(event) {
const url = new URL(event.request.url)
// Redirects
const redirects = {
'bitgravity': 'tata-communications',
'highwinds': 'stackpath',
'maxcdn': 'stackpath',
'netdna': 'stackpath',
'level3': 'centurylink',
'/blog/feed/': '/blog/feed.xml',
'/blogdef/': '/social/blog/',
'/geodef/': '/social/geo/',
'/guidedef/': '/social/guides/',
'/blog/akamai-down/': '/blog/'
}
let target = null
for (const source in redirects) {
if (url.pathname.includes(source)) {
if(target == null){
target = new URL(url.href)
}
target = new URL(target.href.replace(source, redirects[source]))
}
}
if (target != null){
return Response.redirect(target, 301)
}
}
At the end of your function:
if (target != null){
return Response.redirect(target, 301)
}
}
If target is null -- meaning no redirects matched -- then you are not returning any value. This will produce an exception (error 1101). When running in preview, the JS console will show the error: "Uncaught (in response) TypeError: Failed to execute function: parameter 1 is not of type 'Response'."
You probably want to change the code to:
if (target != null){
return Response.redirect(target, 301)
}
return fetch(event.request)
}
Of course, your worker also needs this at the top (but I assume you just left it out for brevity):
addEventListener("fetch", event => {
return event.respondWith(handleEvent(event))
})

Phantomjs Automation of a website leads me to getting IP blocked

I'm using PhantomJS to automate a page. What I do is:
do{
console.log(i);
i++;
page.open(url);
do { phantom.page.sendEvent('mousemove'); } while (page.loading);
if(page.injectJs('./Search.js') == false){
console.log("Search.js Failed")
}
var links = page.evaluate(function(json){
return search(json)
},json)
console.log(links);
} while(links == "")
So this leads me to opening the website repeated until what I'm looking for appears. But this also leads me to getting IP banned. What can I do to get around this?
Your IP is probably getting banned because the script generates too many requests to the website in very little time. So, you need to throttle requests, to apply a pause between them.
I would rewrite your script like this:
var page = require('webpage').create();
var url = "http://www.website.tld/";
var json = {"some" : "json"};
var i = 0;
var links;
// We abstract main code to a function so that we can call it
// again and again from itself
function getlinks (url, json) {
i++;
console.log(i);
page.open(url);
do { phantom.page.sendEvent('mousemove'); } while (page.loading);
if(page.injectJs('./Search.js') == false){
console.log("Search.js Failed")
}
var links = page.evaluate(function(json){
return search(json);
}, json);
if(links == "")
{
// No links scraped yet, so we wait for 3 seconds and try again
setTimeout(function(){
getlinks(url, json);
}, 3000)
}
else
{
console.log(links);
phantom.exit();
}
}
getlinks(url, json);