How to Screenshot a URL in Go

Cloudmersive
1 min readJul 29, 2021

--

One of the primary lures utilized by phishing and other targeted email attacks are malicious URLs; many users have fallen prey to the often legitimate-looking links and ended up with a mess on their device. In this brief tutorial, we will demonstrate how you can use an API in Go to automatically screenshot a URL, allowing you to avoid a potential security disaster.

To call the API function you will need to input your screenshot request parameters including the URL, extra loading wait time, screenshot width, and screenshot height into the below example code:

package mainimport (
"fmt"
"strings"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/convert/web/url/to/screenshot"
method := "POST"
payload := strings.NewReader("Url=%3Cstring%3E&ExtraLoadingWait=%3Cinteger%3E&ScreenshotWidth=%3Cinteger%3E&ScreenshotHeight=%3Cinteger%3E")client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
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))
}

A PNG screenshot of the full-page image will be promptly returned for review!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

Responses (1)