Json and HTTP GET request

Hi the aim of this script that actually is not working properly right now (hope you’ll help to figure out why) is to: given an IP to get the ASN and save it into an array. To simplify the review let’s assume the ip is 104.27.149.119 and to find the ASN we will use the api of https://api.iptoasn.com so that it returns a json.

This is the script, probably I have done something wrong in multiple parts of the script, I think also in the structs.

package main

import (
	"encoding/json"
	"fmt"
	"net/http"
)

type asnrecordEntry struct {
	Domain 		bool `json:"announced"`
	CC    		string `json:"as_country_code"`
	Description string `json:"as_description"`
	Type     	int `json:"as_number"`
    First  		string `json:"first_ip"`
    Last 		string `json:"last_ip"`
}

type Response struct {
	Records []asnrecordEntry 
}


func main () {
	url := fmt.Sprintf("https://api.iptoasn.com/v1/as/ip/104.27.149.119")
	asnrecord := make([]asnrecordEntry, 0)

	client := &http.Client{}

	if res, err := http.NewRequest("GET", url, nil); err == nil { 
		res.Header.Set("Accept", `application\json`)
		resp, err := client.Do(res)
		defer resp.Body.Close()

		decoder := json.NewDecoder(resp.Body)
		r := Response{}

		if err = decoder.Decode(&r); err == nil {
			asnrecord = r.Response
		}
	}

	fmt.Println(asnrecord)
}

I simplified your code to figure out how the things works. Basically are some mistakes in the data structure (take care of returned data types) and I can’t figure out why would you need an array for the response. Anyway bellow is a functional code.

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
)

type AsnrecordEntry struct {
	Domain      bool   `json:"announced"`
	CC          string `json:"as_country_code"`
	Description string `json:"as_description"`
	Type        int    `json:"as_number"`
	First       string `json:"first_ip"`
	Last        string `json:"last_ip"`
}

func main() {
	url := fmt.Sprintf("https://api.iptoasn.com/v1/as/ip/104.27.149.119")

	client := &http.Client{}

	res, err := http.NewRequest("GET", url, nil)
	if err != nil {
		log.Println(err)
		return
	}

	res.Header.Set("Accept", `application\json`)
	resp, err := client.Do(res)
    if err != nil {
    	log.Println(err)
    	return
    }
	defer resp.Body.Close()

	var asnrecord AsnrecordEntry
	if err := json.NewDecoder(resp.Body).Decode(&asnrecord); err != nil {
		log.Println(err)
		return
	}

	fmt.Println(asnrecord)
}

the response

{true US CLOUDFLARENET - Cloudflare, Inc. 13335 104.23.208.0 104.31.95.255}

LE: Although err == nil is still ok, I handled the error in a more idiomatic way.

2 Likes

Thanks very much man, I do really appreciated your help a lot, just one more question: I have to save the value of the ASN that in this case is 13335 into a variable, how can I do that?

Well, after you have decoded the response you already have a variable of AsnrecordEntry type . All you need to do is to access the corresponding field, eg:

type := asnrecord.Type
1 Like

You need to check err here

1 Like

You are right, I updated the example. Thanks.