Code to download file from URL does not give clean file

Hello,
I have code which downloads json file from github.com, but when I check the file on my local machine it shows unnecessary stuff attached to the file, I just need the original file as shown in https://raw.githubusercontent.com/f5devcentral/f5-tetration/master/scripts/Tetration_TCP_L4_ipfix.json

if I open the file on my local machine it gives something like that …at the start

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
  <link rel="dns-prefetch" href="https://assets-cdn.github.com">
  <link rel="dns-prefetch" href="https://avatars0.githubusercontent.com">
  <link rel="dns-prefetch" href="https://avatars1.githubusercontent.com">
  <link rel="dns-prefetch" href="https://avatars2.githubusercontent.com">
  <link rel="dns-prefetch" href="https://avatars3.githubusercontent.com">
  <link rel="dns-prefetch" href="https://github-cloud.s3.amazonaws.com">
  <link rel="dns-prefetch" href="https://user-images.githubusercontent.com/">

Here is the code

fileTCPexists := fileTCPexists() // returns true or false
	if fileTCPexists {
		fmt.Println(" TCP File exists on your local machine")

	} else {
		fmt.Println(" irule does not exists on local machine ..... getting from github")
		downloadTCPiruleFromGithub()
	}

func downloadTCPiruleFromGithub() bool {
	fmt.Println(" Downloading from github ........")
	fileUrl := "https://github.com/f5devcentral/f5-tetration/blob/master/scripts/Tetration_TCP_L4_ipfix.json"

	err := DownloadFile("irules/Tetration_TCP_L4_ipfix.json", fileUrl)
	if err != nil {
		panic(err)
	}
	return true
}

  
//DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
func DownloadFile(filepath string, url string) error {

	// Create the file
	out, err := os.Create(filepath)
	if err != nil {
		return err
	}
	defer out.Close()

	// Get the data
	resp, err := http.Get(url)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	// Write the body to file
	_, err = io.Copy(out, resp.Body)
	if err != nil {
		return err
	}

	return nil
}

Try using the raw.githubusercontent.com URL instead of the github.com URL.

URL’s pointing to github.com, even if ending in file names, always load the complete Web page containing the file. (Paste the URL from your code sample into a browser address bar to see what I mean.)

Thats the HTMLified URL, why don’t you use the raw URL here, that you used in your introduction?

1 Like

“Thanks this worked”

Thanks this worked