예제 #1
0
func main() {
	//
	// Struct to hold error response
	//
	e := struct {
		Message string
	}{}

	// Start Session
	s := napping.Session{}
	url := "http://httpbin.org/user-agent"
	fmt.Println("URL:>", url)

	res := ResponseUserAgent{}
	resp, err := s.Get(url, nil, &res, nil)
	if err != nil {
		log.Fatal(err)
	}
	//
	// Process response
	//
	println("")
	fmt.Println("response Status:", resp.Status())

	if resp.Status() == 200 {
		// fmt.Printf("Result: %s\n\n", resp.response)
		// resp.Unmarshal(&e)
		fmt.Println("res:", res.Useragent)
	} else {
		fmt.Println("Bad response status from Github server")
		fmt.Printf("\t Status:  %v\n", resp.Status())
		fmt.Printf("\t Message: %v\n", e.Message)
	}
	fmt.Println("--------------------------------------------------------------------------------")
	println("")

	url = "http://httpbin.org/get"
	fmt.Println("URL:>", url)
	fooParams := napping.Params{"foo": "bar"}
	p := fooParams

	res = ResponseUserAgent{}
	resp, err = s.Get(url, &p, &res, nil)
	if err != nil {
		log.Fatal(err)
	}
	//
	// Process response
	//
	println("")
	fmt.Println("response Status:", resp.Status())
	fmt.Println("--------------------------------------------------------------------------------")
	fmt.Println("Header")
	fmt.Println(resp.HttpResponse().Header)
	fmt.Println("--------------------------------------------------------------------------------")
	fmt.Println("RawText")
	fmt.Println(resp.RawText())
	println("")
}
예제 #2
0
func main() {
	//
	// Prompt user for Github username/password
	//
	var username string
	fmt.Printf("Github username: "******"%s", &username)
	if err != nil {
		log.Fatal(err)
	}
	passwd, err := gopass.GetPass("Github password: "******"scopes"`
		Note   string   `json:"note"`
	}{
		Scopes: []string{"public_repo"},
		Note:   "testing Go napping" + time.Now().String(),
	}
	//
	// Struct to hold response data
	//
	res := struct {
		Id        int
		Url       string
		Scopes    []string
		Token     string
		App       map[string]string
		Note      string
		NoteUrl   string `json:"note_url"`
		UpdatedAt string `json:"updated_at"`
		CreatedAt string `json:"created_at"`
	}{}
	//
	// Struct to hold error response
	//
	e := struct {
		Message string
		Errors  []struct {
			Resource string
			Field    string
			Code     string
		}
	}{}
	//
	// Setup HTTP Basic auth for this session (ONLY use this with SSL).  Auth
	// can also be configured on a per-request basis when using Send().
	//
	s := napping.Session{
		Userinfo: url.UserPassword(username, passwd),
	}
	url := "https://api.github.com/authorizations"
	//
	// Send request to server
	//
	resp, err := s.Post(url, &payload, &res, &e)
	if err != nil {
		log.Fatal(err)
	}
	//
	// Process response
	//
	println("")
	if resp.Status() == 201 {
		fmt.Printf("Github auth token: %s\n\n", res.Token)
	} else {
		fmt.Println("Bad response status from Github server")
		fmt.Printf("\t Status:  %v\n", resp.Status())
		fmt.Printf("\t Message: %v\n", e.Message)
		fmt.Printf("\t Errors: %v\n", e.Message)
		pretty.Println(e.Errors)
	}
	println("")
}