How to Set a Canonical URL in Go

If you’ve been experiencing issues with users viewing duplicate content via inaccurate search results, we have a solution that may be able to help. By utilizing the following API in Go, you will be able to automatically set the canonical URL of an HTML document — this will inform search engines and other indexers which pages are duplicates of the preferred (canonical) URL.

All you will need for the process are the HTML file or file URL, the canonical URL to set, and your API key, all of which you will input into the below example code:

package mainimport (
"fmt"
"strings"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/convert/edit/html/head/set/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("canonicalUrl", "<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))
}

With this one quick action, you will have eliminated your duplicate content problem and increased the quality of search results for potential users. If you need to obtain an API key, you can do so by registering for a free account on the Cloudmersive website; this will provide 800 calls/month across any of our APIs.

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