top of page

How to Validate Domain Name in Go(Golang) and C/C++?

If you or any of your peers have ever been the target of a phishing attempt, you know that it can be dangerous to click on an unknown domain name; with one wrong click, the security of you or your company’s information can be infiltrated. By using the following API in Go, you can instantly check whether a domain name is valid; the API does the work for you by connecting with DNS services to perform a live validation.



All you need for this simple operation is the target domain name, which you can input into the following code:

package mainimport (
     "fmt"
     "strings"
     "net/http"
     "io/ioutil"
)func main() {url := "https://api.cloudmersive.com/validate/domain/check"
     method := "POST"payload := strings.NewReader(`"<string>"`)client := &http.Client {
     }
     req, err := http.NewRequest(method, url, payload)if err != nil {
          fmt.Println(err)
          return
     }
     req.Header.Add("Content-Type", "application/json")
     req.Header.Add("Apikey", "YOUR-API-KEY-HERE")res, err := client.Do(req)
     if err != nil {
          fmt.Println(err)
          return
     }
     defer res.Body.Close()body, err := ioutil.ReadAll(res.Body)
     if err != nil {
          fmt.Println(err)
          return
     }
     fmt.Println(string(body))
}

Cloudmersive website - You can retrieve your API key from the site by registering for a free account; this will give you access to 800 monthly calls across our library of APIs.


Validate Domain Name in C/C++

The process is achieved by contacting DNS services to perform a live validation, which will ensure that the response is consistently current and accurate. By performing this action, you can instantly find out if the input domain name is correct.


To start things off, we will need to install libcurl into the project:

libcurl/7.75.0

Next, we can call the validation function with the following code:

CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
     curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
     curl_easy_setopt(curl, CURLOPT_URL, "https://api.cloudmersive.com/validate/domain/check");
     curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
     curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
     struct curl_slist *headers = NULL;
     headers = curl_slist_append(headers, "Content-Type: application/json");
     headers = curl_slist_append(headers, "Apikey: YOUR-API-KEY-HERE");
     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
     const char *data = "\"<string>\"";
     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
     res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);

Done! Your result will indicate the validity of the domain name in question.




Source: Medium; cloudmersive


The Tech Platform

0 comments
bottom of page