본문 바로가기
CS/Go

[Go programming] Requesting http get method to an url from an argument

by Warehaus 2023. 6. 24.

 

사진: UnsplashSergey Zolkin

 

 

 

Today, I made a test program to get response by requesting http get method.

 

Here's the sample code.

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
)

func main() {
	for _, url := range os.Args[1:] {
		// Request http get with given url from the first argument.
		resp, err := http.Get(url)
		if err != nil {
			fmt.Fprintf(os.Stderr, "fetch: %v\n", err)
			os.Exit(1)
		}
		// read body of response.
		b, err := ioutil.ReadAll(resp.Body)
		resp.Body.Close()
		if err != nil {
			fmt.Fprintf(os.Stderr, "fetch: reading %s : %v\n", url, err)
			os.Exit(1)
		}
		fmt.Printf("%s", b)
	}
}

 

 

Ok then, let's build the .go file to execute it.

 

> go build http_get.go

 

After that, run the executable with specified url "http://google.com"

 

> ./http_get http://google.com
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="ko"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title><script nonce="j1XtvYgU5jirckuh0V9mLQ">(function(){var _g={kEI:'QXaVZPrID_6C2roPg_qosAM',kEXPI:'0,18167,1341242,6058,207,4804,2316,383,246,5,1129120,1197754,647,223,302338,77529,16114,28684,22431,1361,12320,17579,4998,17075,38444,2872,2891,11754,606,30668,27618,2404,16105,230,20583,4,1528,2301,38936,3192,13660,4437,22616,6621,7596,1,42154,2,38003,1758,6699,31123,4567,6253,23424,1252,5835,14968,4332,19,7465,445,2,2,1,26632,8155,7381,15969,873,19634,7,1922,9779,42459,20198,928,19209,14,82,7651,12555,3371,5006,18988,550,4825,1536,1494,3034,3076,9706,1804,10472,2884,9481,4256,2258,3746,1896,9062,1330,1697,1632,7951,5594,1444,5905,5808,1149,980,5785,1047,2831,1073,5711,6221,970,301,2041,372,1776,5202601,6751,224,2,61,116,8797968,3311,141,795,19735,1,1,346,6849,36,27,19,23943553,397,182,4043528,16673,2893,6250,19852,29,7,4904,1824,2,48,94,1404036,23759270,994,2607,255,292,1018,2,1153,2108,1970,1160,218,1022,34,387,415,1532,14,15,459,89,1442,1094,287,1769,14,548,2227,99,174,66,623,515,336,442,107,197,148,438,213,286,1870,536,592,31,1099,123,1,872,433,6,353,310,18,85,167,91,404,401,206,635,532,130,73,170,254,475,325,125,537,664,663,34,58,227,17,721,23,791,216,406,76,782,120,118,38,980,122,1089,4,237,83,799,148,468,1,41,5,2,4,793,694,530,848,989,55,170,116,3,541,343,404,244,1150,593,16,540,736,85,383',kBL:'EIIf',kOPI:89978449};if (window.google && window.google.stvsc){window.google.kEI = _g.kEI;}else {window.google = _g;}
})();(function(){google.sn='webhp';google.kHL='ko';})();(function(){
var h=this||self;function l(){return void 0!==window.google&&void 0!==window.google.kOPI&&0!==window.google.kOPI?window.google.kOPI:null};var m,n=[];function p(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));)a=a.parentNode;return b||m}function q(a){for(var b=null;a&&(!a.getAttribute||!(b=a.getAttribute("leid")));)a=a.parentNode;return b}function r(a){/^http:/i.test(a)&&"https:"===window.location.protocol&&(google.ml&&google.ml(Error("a"),!1,{src:a,glmm:1}),a="");return a}
function t(a,b,c,d,k){var e="";-1===b.search("&ei=")&&(e="&ei="+p(d),-1===b.search("&lei=")&&(d=q(d))&&(e+="&lei="+d));d="";var g=-1===b.search("&cshid=")&&"slh"!==a,f=[];f.push(["zx",Date.now().toString()]);h._cshid&&g&&f.push(["cshid",h._cshid]);c=c();null!=c&&f.push(["opi",c.toString()]);for(c=0;c<f.length;c++){if(0===c||0<c)d+="&";d+=f[c][0]+"="+f[c][1]}return"/"+(k||"gen_204")+"?atyp=i&ct="+String(a)+"&cad="+(b+e+d)};m=google.kEI;google.getEI=p;google.getLEI=q;google.ml=function(){return null};google.log=function(a,b,c,d,k,e){e=void 0===e?l:e;c||(c=t(a,b,e,d,k));if(c=r(c)){a=new Image;var g=n.length;n[g]=a;a.onerror=a.onload=a.onabort=function(){delete n[g]};a.src=c}};google.logUrl=function(a,b){b=void 0===b?l:b;return t("",a,b)};}).call(this);(function(){google.y={};google.sy=[];google.x=function(a,b){if(a)var c=a.id;else{do c=Math.random();while(google.y[c])}google.y[c]=[a,b];return!1};google.sx=function(a){google.sy.push(a)};google.lm=[];google.plm=function(a){google.lm.push.apply(google.lm,a)};google.lq=[];google.load=function(a,b,c){google.lq.push([[a],b,c])};google.loadAll=function(a,b){google.lq.push([a,b])};google.bx=!1;google.lx=function(){};}).call(this);google.f={};(function(){
....

 

As I ran http_get, it requested get method to "http://google.com" and got the html response.

Even though it's a very simple test code, you can start to implement a part of program to request APIs with this sample code.

 

 

Cheers.

 

 

warehouse.