input

package
v2.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2021 License: MIT Imports: 2 Imported by: 0

README

Handling user inputs

  • Cyclops makes it easy to get query and form values.
  • To get values from query parameters, do the following:
import "github.com/flannel-dev-lab/cyclops/v2/input"

func Login(w http.ResponseWriter, r *http.Request) {
    username := input.Query("username", r)
}
  • The above code will capture query parameter http://example.com?username="cyclops"
  • To get values from form data, do the following:
import "github.com/flannel-dev-lab/cyclops/v2/input"

func Login(w http.ResponseWriter, r *http.Request) {
    username := input.Form("username", r)
}

Handling File Uploads

Cyclops makes it seamless to handle file uploads using the method input.FileContent(r *http.Request, key string). Here the key parameter is the name of the html tag to expect file content. All the user needs to do is to make sure that the form encoding supports multipart data. Consider the below example which describes how file handling works

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>A static page</title>
    <link rel="stylesheet" href="/static/stylesheets/main.css" type="text/css">
</head>
<body>
<h1>Hello from a static page</h1>

<h1>Contact</h1>
<form action="/hello" method="POST" enctype="multipart/form-data">
    <input type="file" name="uploadfile" />
    <input type="submit">
</form>

</body>
</html>
func FileHandler(w http.ResponseWriter, r *http.Request) {
	file, _, err := input.FileContent(r, "uploadfile")
	if err != nil {
		log.Fatal(err)
		return
	}

	f, err := os.OpenFile("test.txt", os.O_WRONLY|os.O_CREATE, 0666)
	if err != nil {
		log.Fatal(err)
		return
	}
	defer f.Close()
	io.Copy(f, file)

	return
}

Documentation

Overview

input deals with getting the values from form and query parameters

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FileContent

func FileContent(r *http.Request, key string) (multipart.File, *multipart.FileHeader, error)

FileContent retrieves the file contents from the requests, else returns error

func Form

func Form(key string, request *http.Request) string

Form retrieves the form parameters from the request

func Query

func Query(key string, request *http.Request) string

Query retrieves the url parameters from the request

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL