Golang Request.Form Examples

Golang Request.Form - 29 examples found. These are the top rated real world Golang examples of net/http.Request.Form extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func RegisterHandler(w http.ResponseWriter, r *http.Request) {

	r.ParseForm()
	fmt.Println(r.Form())
	fmt.Println(r.FormValue("data"))

	data := r.Header.Get("data")
	bytes := []byte(data)
	var user types.User
	json.Unmarshal(bytes, &user)
	// userData := f.(map[string]interface{})
	// fmt.Println("data: ", userData)

	err := parsing.AddUser(user.FacebookID, user.PublicKey)

	var errorResponse types.ErrorResponse
	if err != nil {
		fmt.Printf("Error adding user: %v\n", err)
		errorResponse = types.ErrorResponse{true, err.Error()}
	} else {
		fmt.Printf("Added user: %v\n", user.FacebookID)
		errorResponse = types.ErrorResponse{false, ""}
	}

	//write json to return here
	b, err := json.Marshal(errorResponse)
	fmt.Fprintf(w, "%s", b)
}
Example #2
0
File: main.go Project: wcgh/go_read
func indexHandler(rw http.ResponseWriter, req *http.Request) {
	req.ParseForm()
	if len(req.Form["name"]) > 0 {
		fmt.Fprint(rw, "hello,", req.Form("name")[0])
	} else {
		fmt.Fprint(rw, "hello,world")
	}
}
Example #3
0
/**
 *	测试session manager的Free()
 *	测试时间20秒,需要耐心等待
 */
func TestSessionManagerFree(t *testing.T) {
	runtime.GOMAXPROCS(runtime.NumCPU())
	sm := NewSessionManagerAtGCTime(1, true)

	sm.testing = true

	fmt.Println("sm free() testing...:", time.Now(), "\n")

	go func() {

		//	添加http session
		for i := 1; i <= 10; i++ {
			rw := TestImplResponse{}
			req := http.Request{}
			req.RemoteAddr = "128.0.0.1:8212"
			req.Header = make(http.Header, 1)
			req.Form = make(url.Values)
			sm.tempSessMaxlifeTime = int32(i)
			_, err := sm.GetSession(rw, &req, int32(i), false)
			if nil != err {
				fmt.Println("get session error:", err)
			}
		}

		<-time.After(time.Duration(6) * time.Second)
		fmt.Println("session manager start free()...:", time.Now())
		sm.Free()
	}()

	time.Sleep(time.Duration(20) * time.Second)
}
Example #4
0
func slashws(w http.ResponseWriter, req *http.Request) {
	// Upgrader.Upgrade() has Origin check if .CheckOrigin is nil
	upgrader := gorillawebsocket.Upgrader{}
	wsconn, err := upgrader.Upgrade(w, req, nil)
	if err != nil { // Upgrade() does http.Error() to the client
		return
	}

	// req.Method == "GET" asserted by the mux
	req.Form = nil // reset reused later .Form
	c := &conn{
		Conn:          wsconn,
		requestOrigin: req,
		receive:       make(chan *received, 2),
		push:          make(chan *pageUpdate, 2),
		full:          defaultClient(),
	}
	register <- c
	defer func() {
		unregister <- c
		c.Conn.Close()
	}()
	stop := make(chan struct{}, 1)
	go c.receiveLoop(stop) // read from the client
	c.updateLoop(stop)     // write to the client
}
Example #5
0
func (req *stockHttp) Request(ret interface{}) error {
	var hdl *http.Request
	var resp *http.Response
	var err error

	if hdl, err = http.NewRequest("GET", req.url+"?"+req.params.Encode(), nil); err != nil {
		return err
	}

	hdl.Form = req.params
	if err = hdl.ParseForm(); err != nil {
		return err
	}

	for k, v := range req.headers {
		hdl.Header.Add(k, v)
	}

	if resp, err = http.DefaultClient.Do(hdl); err != nil {
		return err
	}
	defer resp.Body.Close()

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

	if err = json.Unmarshal(body, ret); err != nil {
		return err
	}

	return nil
}
Example #6
0
func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
	log := handler.logger.Session("index")

	client := handler.clientFactory.Build(r)

	pipelines, err := client.ListPipelines()
	if err != nil {
		log.Error("failed-to-list-pipelines", err)
		return err
	}

	if len(pipelines) == 0 {
		err := handler.noBuildsTemplate.Execute(w, TemplateData{})
		if err != nil {
			log.Fatal("failed-to-build-template", err, lager.Data{})
			return err
		}

		return nil
	}

	if r.Form == nil {
		r.Form = url.Values{}
	}

	r.Form[":pipeline"] = []string{pipelines[0].Name}

	return handler.pipelineHandler.ServeHTTP(w, r)
}