Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A better way of accessing form fields #1578

Open
bigbao9494 opened this issue Jun 3, 2023 · 5 comments
Open

A better way of accessing form fields #1578

bigbao9494 opened this issue Jun 3, 2023 · 5 comments

Comments

@bigbao9494
Copy link

A better way of accessing form fields

  1. Obtain the file uploaded through FormData in the interface [&] (const Request&/* req */, Response&res), and add some additional parameters, it's file_and_param.
  2. Golang has a similar processing interface that handles this issue well. All parameters are in req.params (just read directly), and all files are in req.files.
    req_test.zip
@PixlRainbow
Copy link
Contributor

I would suggest that

  1. req.params be renamed to req.form.fields to make it clear that both multipart and urlencoded text fields are now accessed from the same interface. File or binary attachments would be accessed through req.form.files.
  2. Form parsing (server side) and form encoding (client side) would be delegated to a class Form with subclasses to handle the url encoded and multipart cases.
  3. We don't copy golang http heuristics for determining whether a multipart field is a file attachment, but instead take a more adaptive approach like python urllib3

@bigbao9494
Copy link
Author

This is the usage method in Golang, you can refer to it:

func upload_file(writer http.ResponseWriter, request *http.Request) {

//only FormData
if len(r.MultipartForm.Value) > 0 {
	fmt.Println(r.MultipartForm.Value["hello"])
	fmt.Println(r.MultipartForm.Value["post"])
}
//FormData and URLData
if len(request.Form) > 0 {
	for key, val := range request.Form {
		fmt.Println(key, val, reflect.TypeOf(val))
	}
}

filename := request.MultipartForm.File["file"][0].Filename
fileHeader := request.MultipartForm.File["file"][0]
file, err := fileHeader.Open()

}

@yhirose
Copy link
Owner

yhirose commented Jun 5, 2023

Thanks for the good suggestions!

@bigbao9494
Copy link
Author

@yhirose ,hi yhirose ,how about this featuer?

@yhirose
Copy link
Owner

yhirose commented Jul 8, 2023

@bigbao9494, I don't have the time to work on it right now, but I keep it as 'enhancement'. Of course, a pull request is always welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants