How to Get a Canonical URL in Go

Cloudmersive
2 min readJul 27, 2021

Not sure if the web page you are working with is the preferred version? One way you can ensure you aren’t referring to the most up-to-date and accurate information is to retrieve the canonical URL. These URLs represent the cardinal copy of the page, and decrease the risk of duplicate content. For a quick and easy way to retrieve the canonical URL of an HTML document, you can use the following API in Go — the result will be returned in no time at all.

In order to perform the operation, simply input the target file or file URL, along with your API key, into the below code:

package mainimport (
"fmt"
"strings"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/convert/edit/html/head/get/rel-canonical-url"
method := "POST"
payload := strings.NewReader("inputFile=%3Cbinary%3E")client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("inputFileUrl", "<string>")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
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))
}

To retrieve your API key, visit the Cloudmersive website to register for a free account; this provides 800 monthly calls across our entire library of APIs.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.