We have some URL fields that have REGEX validation on them, so they look like a valid URL.
But we still need to test that the URL actually links to a live page (or live when the record was saved).
I tried doing this client-side with Javascript, but that seems to be impossible (due to various built in browser security features, and that makes sense. You don't want a webpage randomly accessing data from somewhere else you may or may not trust).
So I thought to do it server side. But it seems that the only way Apex can access the internet is via Callouts and for that to work you need to register each site in the Remote Site Settings. Something I cannot do for this use case.
So I need a simple way to just get the HEAD
of a given, arbitrary, URL and see if I get a 200 or not.
This is one of such cases where you need a middleware like mulesoft or something similar made and hosted on public IP.
You will call a certain endpoint with JSON data of the URL you wanna validate, that 3rd party middleware will proxy and do callout to the URL you provided and return if URL is valid or not.
We found a way to do this on the client side, using jQuery ajax
and jsonp
.
function urlExists(url){
var myPromise = new Promise(function(resolve, reject){
$.ajax({
url:url,
dataType: 'jsonp',
success:function(json){
},
error:function(err){ //will fail because of CORS
if (err && err.status && err.status === 200){
resolve(err.status);
}
else{
reject(err.status);
}
}
});
});
return myPromise;