예제 #1
0
func (s *sessionCreator) RequestToken() error {
	pubPKIXBytes, err := x509.MarshalPKIXPublicKey(&s.pvtKey.PublicKey)
	if err != nil {
		return err
	}

	c := new(http.Client)
	req := request.NewRequest(c)
	req.Json = &dtos.GetTokenRequestDto{pubPKIXBytes}

	url := combineServerUrl(s.baseServerUrl, "/token")
	resp, err := req.Post(url)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		return fmt.Errorf("Code: %d - %s", resp.StatusCode, resp.Status)
	}

	responseBytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}

	dto := &dtos.GenTokenResponseDto{}
	err = json.Unmarshal(responseBytes, dto)
	if err != nil {
		return err
	}

	s.dto = dto
	return nil
}
예제 #2
0
파일: proxy.go 프로젝트: disunbow/request
func httpProxy2(URL string) {
	c := &http.Client{}
	req := request.NewRequest(c)
	req.Proxy = URL
	resp, err := req.Get("http://httpbin.org/get")
	fmt.Println(err)
	fmt.Println(resp.Text())
}
예제 #3
0
파일: timeout.go 프로젝트: disunbow/request
func main() {
	c := new(http.Client)
	req := request.NewRequest(c)

	fmt.Println("default timeout")
	diff(req)

	timeout := time.Duration(1 * time.Second)
	c.Timeout = timeout
	fmt.Printf("set timeout = %f seconds\n", timeout.Seconds())
	diff(req)

	// Or use req.Client
	c = new(http.Client)
	req = request.NewRequest(c)
	req.Client.Timeout = timeout
	fmt.Printf("set timeout = %f seconds\n", timeout.Seconds())
	diff(req)
}
예제 #4
0
func ExamplePostRawBody() {
	c := new(http.Client)
	req := request.NewRequest(c)
	req.Body = strings.NewReader("a=1&b=2&foo=bar")
	req.Headers = map[string]string{
		"Content-Type": request.DefaultContentType,
	}
	url := "http://httpbin.org/post"
	resp, _ := req.Post(url)
	defer resp.Body.Close()
}
예제 #5
0
func ExampleGet_cookies() {
	c := new(http.Client)
	req := request.NewRequest(c)
	req.Cookies = map[string]string{
		"name": "value",
		"foo":  "bar",
	}
	url := "http://httpbin.org/cookies"
	resp, _ := req.Get(url)
	defer resp.Body.Close()
}
예제 #6
0
func ExamplePost() {
	c := new(http.Client)
	req := request.NewRequest(c)
	req.Data = map[string]string{
		"a": "1",
		"b": "2",
	}
	url := "http://httpbin.org/post"
	resp, _ := req.Post(url)
	defer resp.Body.Close()
}
예제 #7
0
파일: proxy.go 프로젝트: disunbow/request
func httpProxy(URL string) {
	proxyURL, _ := url.Parse(URL)
	tr := &http.Transport{
		Proxy: http.ProxyURL(proxyURL),
	}
	c := &http.Client{Transport: tr}
	req := request.NewRequest(c)
	resp, err := req.Get("http://httpbin.org/get")
	fmt.Println(err)
	fmt.Println(resp.Text())
}
예제 #8
0
func ExamplePost_files() {
	c := new(http.Client)
	req := request.NewRequest(c)
	f, _ := os.Open("test.txt")
	defer f.Close()
	req.Files = []request.FileField{
		request.FileField{"abc", "abc.txt", f},
	}
	url := "http://httpbin.org/post"
	resp, _ := req.Post(url)
	defer resp.Body.Close()
}
예제 #9
0
func ExampleGet() {
	c := new(http.Client)
	req := request.NewRequest(c)
	url := "http://httpbin.org/get"
	resp, _ := req.Get(url)
	d, _ := resp.Json()
	defer resp.Body.Close()
	fmt.Println(resp.Ok())
	fmt.Println(d.Get("url").MustString())
	// Output:
	//true
	//http://httpbin.org/get
}
예제 #10
0
func main() {
	flag.Parse()
	log.SetFlags(0)
	hipToken := os.Getenv("HIPLOG_TOKEN")
	if hipToken == "" {
		log.Fatal("Need to set HIPLOG_TOKEN")
	}
	roomID := flag.Arg(0)
	// TODO: make it flag
	c := new(http.Client)
	URL := fmt.Sprintf("https://api.hipchat.com/v2/room/%s/webhook", roomID)
	req := request.NewRequest(c)
	req.Headers = map[string]string{
		"Content-Type": "application/json",
	}
	req.Params = map[string]string{
		"auth_token": hipToken,
	}
	var err error
	var resp *request.Response
	if *command == "create" {
		if *webhookURL == "" {
			log.Fatal("Need to specify webhook_url")
		}
		req.Json = map[string]string{
			"url":   *webhookURL,
			"name":  roomID,
			"event": *eventType,
		}
		resp, err = req.Post(URL)
	} else if *command == "query" {
		resp, err = req.Get(URL)
	} else if *command == "delete" {
		if *webhookID == "" {
			log.Fatal("Need to specify webhook_id")
		}
		URL := fmt.Sprintf("%s/%s", URL, *webhookID)
		resp, err = req.Delete(URL)
	} else {
		log.Fatal("Must specify the correct command")
	}
	if err != nil {
		defer resp.Body.Close()
		log.Printf("%s failed, err: %v", *command, err)
	} else {
		log.Printf("%s success", *command)
		fmt.Println(resp.Json())
	}
}
예제 #11
0
func ExampleGet_params() {
	c := new(http.Client)
	req := request.NewRequest(c)
	req.Params = map[string]string{
		"a": "1",
		"b": "2",
	}
	url := "http://httpbin.org/get"
	resp, _ := req.Get(url)
	d, _ := resp.Json()
	defer resp.Body.Close()
	fmt.Println(d.Get("url").MustString())
	// Output:
	//http://httpbin.org/get?a=1&b=2
}
예제 #12
0
func ExampleGet_customHeaders() {
	c := new(http.Client)
	req := request.NewRequest(c)
	req.Headers = map[string]string{
		"X-Abc":      "abc",
		"User-Agent": "go-request-test",
	}
	url := "http://httpbin.org/get"
	resp, _ := req.Get(url)
	d, _ := resp.Json()
	defer resp.Body.Close()
	fmt.Println(d.Get("headers").Get("User-Agent").MustString())
	fmt.Println(d.Get("headers").Get("X-Abc").MustString())
	// Output:
	//go-request-test
	//abc
}
예제 #13
0
파일: frisby.go 프로젝트: verdverm/frisby
// Creates a new Frisby object with the given name.
//
// The given name will be used if you call PrintReport()
func Create(name string) *Frisby {
	F := new(Frisby)
	F.Name = name
	F.Req = request.NewRequest(new(http.Client))
	F.Errs = make([]error, 0)

	// copy in global settings
	F.Req.BasicAuth = Global.Req.BasicAuth
	F.Req.Proxy = Global.Req.Proxy
	F.SetHeaders(Global.Req.Headers)
	F.SetCookies(Global.Req.Cookies)
	F.SetDatas(Global.Req.Data)
	F.SetParams(Global.Req.Params)
	F.Req.Json = Global.Req.Json
	F.Req.Files = append(F.Req.Files, Global.Req.Files...)

	return F
}
예제 #14
0
파일: login.go 프로젝트: disunbow/request
func main() {
	c := new(http.Client)
	req := request.NewRequest(c)
	log.Println(home(req)) // 403

	// login
	csrfToken, err := getCSRFToken(req)
	if err != nil {
		log.Fatal(err)
	}
	req.Data = map[string]string{
		"csrfmiddlewaretoken": csrfToken,
		"name":                "go-request",
		"password":            "******",
	}
	log.Println(csrfToken)
	err = login(req)
	if err != nil {
		log.Fatal(err)
	}

	log.Println(home(req)) // 200
}
예제 #15
0
파일: global.go 프로젝트: jacobxk/frisby
func init() {
	Global.Req = request.NewRequest(new(http.Client))
	Global.Errs = make(map[string][]error, 0)
	Global.PrintProgressDot = true
}
예제 #16
0
func (s *session) NewRequest() *request.Request {
	c := s.newHttpClient()
	req := request.NewRequest(c)
	req.Headers["Authorization"] = "Bearer " + fmt.Sprintf("sid-%d", s.sessionId)
	return req
}