@kjk1/

gobook http client user agent

Go

No description

fork
loading
Files
  • main.go

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.5012563910955075","path":"main.go","file":{"path":"main.go","content":{"asEncoding":{"base64":"Ly8gbm8gcGxheWdyb3VuZApwYWNrYWdlIG1haW4KCmltcG9ydCAoCgkiZm10IgoJImlvL2lvdXRpbCIKCSJsb2ciCgkibmV0L2h0dHAiCgkidGltZSIKKQoKZnVuYyBtYWluKCkgewoJLy8gOnNob3cgc3RhcnQKCXVzZXJBZ2VudCA6PSAiU3BlY2lhbCBBZ2VudCB2IDEuMC4xNiIKCgljbGllbnQgOj0gJmh0dHAuQ2xpZW50e30KCWNsaWVudC5UaW1lb3V0ID0gdGltZS5TZWNvbmQgKiAxNQoKCXVyaSA6PSAiaHR0cHM6Ly9odHRwYmluLm9yZy91c2VyLWFnZW50IgoJcmVxLCBlcnIgOj0gaHR0cC5OZXdSZXF1ZXN0KGh0dHAuTWV0aG9kR2V0LCB1cmksIG5pbCkKCWlmIGVyciAhPSBuaWwgewoJCWxvZy5GYXRhbGYoImh0dHAuTmV3UmVxdWVzdCgpIGZhaWxlZCB3aXRoICclcydcbiIsIGVycikKCX0KCXJlcS5IZWFkZXIuU2V0KCJVc2VyLUFnZW50IiwgdXNlckFnZW50KQoJcmVzcCwgZXJyIDo9IGNsaWVudC5EbyhyZXEpCglpZiBlcnIgIT0gbmlsIHsKCQlsb2cuRmF0YWxmKCJjbGllbnQuRG8oKSBmYWlsZWQgd2l0aCAnJXMnXG4iLCBlcnIpCgl9CgoJZGVmZXIgcmVzcC5Cb2R5LkNsb3NlKCkKCWQsIGVyciA6PSBpb3V0aWwuUmVhZEFsbChyZXNwLkJvZHkpCglpZiBlcnIgIT0gbmlsIHsKCQlsb2cuRmF0YWxmKCJpb3V0aWwuUmVhZEFsbCgpIGZhaWxlZCB3aXRoICclcydcbiIsIGVycikKCX0KCglmbXQuUHJpbnRmKCJSZXNwb25zZSBzdGF0dXMgY29kZTogJWQsIHRleHQ6XG4lc1xuIiwgcmVzcC5TdGF0dXNDb2RlLCBzdHJpbmcoZCkpCgkvLyA6c2hvdyBlbmQKfQ=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// no playground
package main

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

func main() {
	// :show start
	userAgent := "Special Agent v 1.0.16"

	client := &http.Client{}
	client.Timeout = time.Second * 15

	uri := "https://httpbin.org/user-agent"
	req, err := http.NewRequest(http.MethodGet, uri, nil)
	if err != nil {
		log.Fatalf("http.NewRequest() failed with '%s'\n", err)
	}
	req.Header.Set("User-Agent", userAgent)
	resp, err := client.Do(req)
	if err != nil {
		log.Fatalf("client.Do() failed with '%s'\n", err)
	}

	defer resp.Body.Close()
	d, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatalf("ioutil.ReadAll() failed with '%s'\n", err)
	}

	fmt.Printf("Response status code: %d, text:\n%s\n", resp.StatusCode, string(d))
	// :show end
}