LoginSignup
0
0

More than 1 year has passed since last update.

Golang の http クライアントの使い方 (Get)

Last updated at Posted at 2020-02-24

プログラム

http_get.go
// ---------------------------------------------------------------
//
//	http_get.go
//
//					Feb/24/2020
// ---------------------------------------------------------------
package main

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

// ---------------------------------------------------------------
func main() {
	fmt.Printf ("*** 開始 ***\n")
	url_target := "https://httpbin.org/get"
	res, err := http.Get(url_target)
	if err != nil {
		fmt.Println("Request error:", err)
		return
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println("Request error:", err)
		return
	}

	str_json := string(body)
	fmt.Println(str_json)

	fmt.Printf ("*** 終了 ***\n")
}

// ---------------------------------------------------------------

実行結果

$ go run http_get.go
*** 開始 ***
{
  "args": {}, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Host": "httpbin.org", 
    "User-Agent": "Go-http-client/2.0", 
    "X-Amzn-Trace-Id": "Root=1-5e532a2d-03367851ed2afcb58a0a452d"
  }, 
  "origin": "163.49.213.57", 
  "url": "https://httpbin.org/get"
}

*** 終了 ***

確認したバージョン

$ go version
go version go1.19.2 linux/amd64
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0