Golang Request.ParseForm Examples

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

	vars := mux.Vars(r)
	r.ParseForm()

	typ := vars["type"]

	if len(typ) == 0 {
		w.WriteHeader(400)
		return
	}

	pStart, pEnd := r.Form.Get("start"), r.Form.Get("end")

	if len(pStart) == 0 || len(pEnd) == 0 {
		w.WriteHeader(400)
		return
	}

	// parse the parameters

	start, end := parseTime(pStart), parseTime(pEnd)

	if start.IsZero() || end.IsZero() {
		w.WriteHeader(400)
		return
	}

	// get the data

	values, err := service.GetValuesRange(c.App, start, end, typ)
	if err != nil {
		log.Println("error: while getting values range:", err.Error())
		w.WriteHeader(500)
		return
	}

	// render the data

	var resp getGraphResponse
	resp.Values = make([]timedValue, len(values))

	for i, v := range values {
		resp.Values[i] = timedValue{
			T: v.Time,
			V: v.Value,
		}
	}

	data, err := json.Marshal(resp)
	if err != nil {
		log.Println("error: while marshaling the GetGraph response:", err.Error())
		w.WriteHeader(500)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.Write(data)
}
Example #2
0
func (srv *Server) serveHTTP(w http.ResponseWriter, req *http.Request) {
	req.ParseForm()
	srv.mutex.Lock()
	defer srv.mutex.Unlock()
	action := req.FormValue("Action")
	if action == "" {
		srv.error(w, &iam.Error{
			StatusCode: 400,
			Code:       "MissingAction",
			Message:    "Missing action",
		})
	}
	if a, ok := actions[action]; ok {
		reqId := fmt.Sprintf("req%0X", srv.reqId)
		srv.reqId++
		if resp, err := a(srv, w, req, reqId); err == nil {
			if err := xml.NewEncoder(w).Encode(resp); err != nil {
				panic(err)
			}
		} else {
			switch err.(type) {
			case *iam.Error:
				srv.error(w, err.(*iam.Error))
			default:
				panic(err)
			}
		}
	} else {
		srv.error(w, &iam.Error{
			StatusCode: 400,
			Code:       "InvalidAction",
			Message:    "Invalid action: " + action,
		})
	}
}
Example #3
0
// handler echos the Path component of the requested URL.
func handler(w http.ResponseWriter, r *http.Request) {
	mu.Lock()
	count++
	mu.Unlock()
	fmt.Fprintf(w, "URL.Path = %q\n", r.URL.Path)
	fmt.Fprintf(w, "%s %s %s\n", r.Method, r.URL, r.Proto)
	for k, v := range r.Header {
		fmt.Fprintf(w, "Header[%q] = %q\n", k, v)
	}
	fmt.Fprintf(w, "Host = %q\n", r.Host)
	fmt.Fprintf(w, "RemoteAddr = %q\n", r.RemoteAddr)
	if err := r.ParseForm(); err != nil {
		log.Print(err)
	}
	for k, v := range r.Form {
		fmt.Fprintf(w, "Form[%q] = %q\n", k, v)
		// http://localhost:8000/?cycles=20
		if k == "cycles" {
			cycles := strings.Join(v, "")
			ncycles, _ := strconv.Atoi(cycles)
			count = count + ncycles
			switch coinflip() {
			case "heads":
				count++
			case "tails":
				count--
			default:
				fmt.Fprintf(w, "yo dawg %d\n", count)
			}
		}
	}
}
Example #4
0
func adminornot(w http.ResponseWriter, q *http.Request) {
	err := q.ParseForm()

	if err != nil {
		// Handle error
		fmt.Println(err)
	}

	decoder := schema.NewDecoder()
	adm := new(authUser)

	decoder.Decode(adm, q.PostForm)

	if adm.checkstaffloginpassword() == true {
		session, _ := store.Get(q, sessionname)
		ulin := staffLogedIn{
			UserLogin:     adm.Login,
			IsUserLogedIn: true,
		}

		session.Values["staffLogedIn"] = ulin

		session.Save(q, w)
		render(w, "admin", nil)
	} else {
		newmap := map[string]interface{}{"metatitle": "Registration", "errormessage": "Wrong login or password"}
		render(w, "adminlogin", newmap)
	}

}
Example #5
0
// GET /events
func getEvents(c *context, w http.ResponseWriter, r *http.Request) {
	if err := r.ParseForm(); err != nil {
		httpError(w, err.Error(), 400)
		return
	}

	var until int64 = -1
	if r.Form.Get("until") != "" {
		u, err := strconv.ParseInt(r.Form.Get("until"), 10, 64)
		if err != nil {
			httpError(w, err.Error(), 400)
			return
		}
		until = u
	}

	c.eventsHandler.Add(r.RemoteAddr, w)

	w.Header().Set("Content-Type", "application/json")

	if f, ok := w.(http.Flusher); ok {
		f.Flush()
	}

	c.eventsHandler.Wait(r.RemoteAddr, until)
}
Example #6
0
// JSON handler for `/delete`:
func deleteJsonHandler(req *http.Request) (result interface{}) {
	if req.Method != "POST" {
		panic(NewHttpError(http.StatusMethodNotAllowed, "Upload requires POST method", fmt.Errorf("Upload requires POST method")))
	}

	// Parse form data:
	if err := req.ParseForm(); err != nil {
		panic(NewHttpError(http.StatusBadRequest, "Error parsing form data", err))
	}
	filename := req.Form.Get("filename")
	if filename == "" {
		panic(NewHttpError(http.StatusBadRequest, "Expecting filename form value", fmt.Errorf("No filename POST value")))
	}

	// Remove the file:
	destPath := path.Join(picsDir, path.Base(filename))
	if err := os.Remove(destPath); err != nil {
		panic(NewHttpError(http.StatusBadRequest, "Unable to delete file", fmt.Errorf("Unable to delete file '%s': %s", destPath, err)))
	}

	return struct {
		Success bool `json:"success"`
	}{
		Success: true,
	}
}