コード例 #1
0
ファイル: holehub.go プロジェクト: ultimate010/holehub
func (hole HoleApp) run(command string) {
	var ro = &grequests.RequestOptions{
		Headers: map[string]string{"Cookie": cookie},
	}

	rsp, err := grequests.Post(hubHost+"/api/holes/"+hole.ID+"/"+command+"/", ro)
	if err != nil {
		log.Fatal(err)
	}
	defer rsp.Close()

	if !rsp.Ok {
		log.Fatalf("Error: %s\n", rsp.String())
	}

	var msg JE
	err = rsp.JSON(&msg)

	if err != nil {
		log.Fatal(err)
	}

	if msg.Code != "0" {
		fmt.Printf("Error: %s\n", msg.Error)
		os.Exit(1)
	}
}
コード例 #2
0
ファイル: main.go プロジェクト: ryanskidmore/GoWork
func main() {
	response, err := grequests.Get("http://127.0.0.1:3000/register", nil)
	if err != nil {
		panic("Unable to register:" + err.Error())
	}
	respdata := strings.Split(response.String(), ",")
	id := respdata[0]
	clienttest := respdata[1]
	worker, err := gowork.NewWorker("w4PYxQjVP9ZStjWpBt5t28CEBmRs8NPx", id, clienttest)
	testresponse, err := grequests.Post("http://127.0.0.1:3000/verify", &grequests.RequestOptions{Params: map[string]string{"id": id, "clientresp": worker.Verification.ClientResponse}})
	if err != nil {
		panic("Unable to verify:" + err.Error())
	}
	worker = worker.SetAuthenticationKey(testresponse.String())
	fmt.Println(worker.SessionAuthenticationKey)
	testresponse, err = grequests.Post("http://127.0.0.1:3000/get_work", &grequests.RequestOptions{Params: map[string]string{"id": id, "sessionauthkey": worker.SessionAuthenticationKey}})
	fmt.Println(testresponse.String())
}
コード例 #3
0
ファイル: client.go プロジェクト: aisk/lean-cli-backup
func (client *Client) post(path string, params map[string]interface{}, options *grequests.RequestOptions) (*simplejson.Json, error) {
	if options == nil {
		options = client.provider.options()
	}
	options.JSON = params
	resp, err := grequests.Post(client.provider.baseURL()+path, options)
	if err != nil {
		return nil, err
	}
	if !resp.Ok {
		return nil, NewErrorFromBody(resp.String())
	}
	return simplejson.NewFromReader(resp)
}
コード例 #4
0
ファイル: example_test.go プロジェクト: wambiri/grequests
func Example_postForm() {
	resp, err := grequests.Post("http://httpbin.org/post",
		&grequests.RequestOptions{Data: map[string]string{"One": "Two"}})

	// This is the basic form POST. The request body will be `one=two`

	if err != nil {
		log.Println("Cannot post: ", err)
	}

	if resp.Ok != true {
		log.Println("Request did not return OK")
	}
}
コード例 #5
0
ファイル: example_test.go プロジェクト: wambiri/grequests
func Example_postJSONAJAX() {
	resp, err := grequests.Post("http://httpbin.org/post",
		&grequests.RequestOptions{
			JSON:   map[string]string{"One": "Two"},
			IsAjax: true, // this adds the X-Requested-With: XMLHttpRequest header
		})

	if err != nil {
		log.Println("Unable to make request", resp.Error)
	}

	if resp.Ok != true {
		log.Println("Request did not return OK")
	}

}
コード例 #6
0
ファイル: holehub.go プロジェクト: ultimate010/holehub
func Login() {
	name, _ := config.Get("email")
	passwd, _ := config.Get("password")
	if name == "" || passwd == "" {
		rdr := bufio.NewScanner(os.Stdin)
		username := ReadLine(rdr, "Username: "******"username", username)
		name := ReadLine(rdr, "Email: ")
		config.Set("email", name)
		passwd := ReadLine(rdr, "Password: "******"password", passwd)
	}

	ro := &grequests.RequestOptions{
		Data: map[string]string{"username": name, "password": passwd},
	}

	rsp, err := grequests.Post(hubHost+"/api/signin/", ro)
	if err != nil {
		log.Fatal(err)
	}
	defer rsp.Close()

	if !rsp.Ok {
		log.Fatalf("Error: %s\n", rsp.String())
	}

	var msg JE
	err = rsp.JSON(&msg)

	if err != nil {
		log.Fatal(err)
	}

	if msg.Code != "0" {
		fmt.Printf("Error: %s\n", msg.Error)
		config.Del("password")
		config.Del("email")
		config.Del("username")
		os.Exit(1)
	} else {
		fmt.Printf("Login HoleHUB %s\n", msg.Message)
		cookie = rsp.Header.Get("Set-Cookie")
		config.Set("cookie", cookie)
	}
}
コード例 #7
0
ファイル: example_test.go プロジェクト: wambiri/grequests
func Example_postXML() {

	type XMLPostMessage struct {
		Name   string
		Age    int
		Height int
	}

	resp, err := grequests.Post("http://httpbin.org/post",
		&grequests.RequestOptions{XML: XMLPostMessage{Name: "Human", Age: 1, Height: 1}})
	// The request body will contain the XML generated by the `XMLPostMessage` struct

	if err != nil {
		log.Println("Unable to make request", resp.Error)
	}

	if resp.Ok != true {
		log.Println("Request did not return OK")
	}
}
コード例 #8
0
ファイル: httpclient_grequest.go プロジェクト: cch123/test
func main() {
	//resp, err := grequests.Get("http://httpbin.org/get", nil)
	//fmt.Println(resp)
	//fmt.Println(err)
	var a = map[string]int{
		"aaa": 1,
		"bbb": 1,
	}
	by, _ := json.Marshal(a)

	var b = bytes.NewReader(by)
	///var a = strings.NewReader("aaa")

	ro := &grequests.RequestOptions{
		RequestBody: b,
		Headers:     map[string]string{"Content-Type": "application/json"},
	}
	resp, err := grequests.Post("http://httpbin.org/post", ro)
	fmt.Println(resp)
	fmt.Println(err)
}
コード例 #9
0
ファイル: holehub.go プロジェクト: ultimate010/holehub
func createHoleApp(scheme, name string) HoleApp {
	if has, _ := appNames.Get(name); has != "" {
		log.Fatalf("App Name: %s is already exists. bind on %s\n", name, has)
	}
	var ro = &grequests.RequestOptions{
		Headers: map[string]string{"Cookie": cookie},
		Data:    map[string]string{"scheme": scheme, "name": name},
	}

	rsp, err := grequests.Post(hubHost+"/api/holes/create/", ro)
	if err != nil {
		log.Fatal(err)
	}
	defer rsp.Close()

	if !rsp.Ok {
		log.Fatalf("Error: %s\n", rsp.String())
	}

	var msg map[string]HoleApp
	err = rsp.JSON(&msg)

	if err != nil {
		log.Fatal(err)
	}

	hole := msg["hole"]
	holes.Set(hole.ID, "name", hole.Name)
	holes.Set(hole.ID, "scheme", hole.Scheme)
	holes.Set(hole.ID, "host", hole.Host)
	holes.Set(hole.ID, "port", hole.Port)
	holes.Set(hole.ID, "status", "stoped")
	apps.Add(hole.ID)
	if hole.Name != "" {
		appNames.Set(hole.Name, hole.ID)
	}

	return hole
}
コード例 #10
0
ファイル: account.go プロジェクト: aisk/lean-cli-backup
// Login LeanCloud account
func Login(email string, password string) (*simplejson.Json, error) {
	options := &grequests.RequestOptions{
		JSON: map[string]string{
			"email":    email,
			"password": password,
		},
	}
	response, err := grequests.Post("https://leancloud.cn/1/signin", options)
	if err != nil {
		return nil, err
	}
	if !response.Ok {
		return nil, NewErrorFromBody(response.String())
	}

	cookies := response.RawResponse.Cookies()

	if err := saveCookies(cookies); err != nil {
		return nil, err
	}

	return simplejson.NewFromReader(response)
}
コード例 #11
0
ファイル: example_test.go プロジェクト: wambiri/grequests
func Example_postFileUpload() {

	fd, err := grequests.FileUploadFromDisk("test_files/mypassword")

	if err != nil {
		log.Println("Unable to open file: ", err)
	}

	// This will upload the file as a multipart mime request
	resp, err := grequests.Post("http://httpbin.org/post",
		&grequests.RequestOptions{
			Files: fd,
			Data:  map[string]string{"One": "Two"},
		})

	if err != nil {
		log.Println("Unable to make request", resp.Error)
	}

	if resp.Ok != true {
		log.Println("Request did not return OK")
	}
}
コード例 #12
0
ファイル: main.go プロジェクト: danoand/gorequests_toml
func main() {
	log.Println("Starting execution...")

	// Create a file to which the program will write
	oFile, oErr := os.Create("outfile.txt")
	if oErr != nil {
		log.Fatalf("ERROR: Error creating an output file see: %v\nEnding processing.", oErr)
	}
	defer oFile.Close()

	// Import the yaml file data
	lErr := loadRequests("data/calldata.toml")
	if lErr != nil {
		log.Fatalf("ERROR: Error loading TOML data see: %v\nEnding processing.", lErr)
	}

	// Iterate through the request data
	for i := 0; i < len(cData.Requests); i++ {
		log.Printf("INFO: Working on request #: %v\n", i+1)
		var madeReq bool

		// Construct the next request
		var myReq grequests.RequestOptions
		var myResp *grequests.Response

		// Assign any parameters
		myReq.Params = cData.Requests[i].Params
		// Determine the method
		myReq.Data = cData.Requests[i].Params
		switch cData.Requests[i].Method {
		case "GET":
			myResp, _ = grequests.Get(cData.Requests[i].URL, &myReq)
			madeReq = true
		case "POST":
			// Configure the ResponseOption for the applicable body and body type
			pErr := roBody(&myReq, &cData.Requests[i])
			if pErr != nil {
				log.Printf("Error: configuring the POST request body\n")
			} else {
				myResp, _ = grequests.Post(cData.Requests[i].URL, &myReq)
				madeReq = true
			}
		default:
			log.Printf("ERROR: Can't determine the request method")
		}

		// Print out the respsonse
		if madeReq == true {
			myRawRespStr, _, _ := utils.DumpResponse(myResp.RawResponse)

			// Format a string to be written to the output file
			oStr := fmt.Sprintf("\n\n******\n\nThe response to #: %v is:\n\n%v\n\n*****\n\n", i, myRawRespStr)

			// Write the output string
			_, wErr := oFile.WriteString(oStr)
			if wErr != nil {
				log.Printf("ERROR: Writing the response to the output file see: %v\n", wErr)
			}
		}
	}

	log.Println("Ending execution...")
}