all repos — honk @ v0.9.1

my fork of honk

toys/gettoken.go (view raw)

 1package main
 2
 3import (
 4	"flag"
 5	"fmt"
 6	"io/ioutil"
 7	"log"
 8	"net/http"
 9	"net/url"
10	"os"
11	"strings"
12)
13
14func main() {
15	server := ""
16	username := ""
17	password := ""
18
19	flag.StringVar(&server, "server", server, "server to connnect")
20	flag.StringVar(&username, "username", username, "username to use")
21	flag.StringVar(&password, "password", password, "password to use")
22	flag.Parse()
23
24	if server == "" || username == "" || password == "" {
25		flag.Usage()
26		os.Exit(1)
27	}
28
29	form := make(url.Values)
30	form.Add("username", username)
31	form.Add("password", password)
32	form.Add("gettoken", "1")
33	loginurl := fmt.Sprintf("https://%s/dologin", server)
34	req, err := http.NewRequest("POST", loginurl, strings.NewReader(form.Encode()))
35	if err != nil {
36		log.Fatal(err)
37	}
38	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
39
40	resp, err := http.DefaultClient.Do(req)
41	if err != nil {
42		log.Fatal(err)
43	}
44	defer resp.Body.Close()
45	answer, err := ioutil.ReadAll(resp.Body)
46	if err != nil {
47		log.Fatal(err)
48	}
49	if resp.StatusCode != 200 {
50		log.Fatalf("status: %d: %s", resp.StatusCode, answer)
51	}
52	fmt.Println(string(answer))
53}