Getting Contents from a URL and converting it into string

Hello, i am developing a cpanel based software ( well it’s not the topic )
actually i want to get contents from a url and want to convert that content to string because i tried by general way it’s giving uint8 byet code.
here is the URL : http://myip.cpanel.net/v1.0/
this url shows our IPv4 Address, i want to read that IPv4 Address in a variable and want to convert that ip to string so that i can use that easily.

actually as i told i am developing a software that requires license and license is based on IP so i want to convert it as string so that i can use that IP easily like showing Your IPv4 Address : fmt.Printf(ip)
if ip == “x.x.x.x” {
fmt.Println(“yap”)
}
else {
fmt.Println(“you’re not allowed”)
}

hope you’re understood, so please tell me a way how can i read contents from that url and convert that content to a string

Use type net.IP, the canonical form for IP addresses.

package main

import (
	"bytes"
	"errors"
	"fmt"
	"io/ioutil"
	"net"
	"net/http"
)

func getMyIP() (net.IP, error) {
	resp, err := http.Get("http://myip.cpanel.net/v1.0/")
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	ip := net.ParseIP(string(bytes.TrimSpace(body)))
	if ip == nil {
		err := errors.New("invalid IP address")
		return nil, err
	}
	return ip, nil
}

func main() {
	myip, err := getMyIP()
	fmt.Println(myip, err)
	if err != nil {
		return
	}

	fmt.Printf("Your IP Address: %s\n", myip)

	allowed := net.ParseIP("77.111.246.40")
	if len(myip) > 0 && myip.Equal(allowed) {
		fmt.Println("yep")
	} else {
		fmt.Println("you’re not allowed")
	}
}

.

77.111.246.40 <nil>
Your IP Address: 77.111.246.40
yep

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.