cpp-httplib icon indicating copy to clipboard operation
cpp-httplib copied to clipboard

A better way of accessing form fields

Open bigbao9494 opened this issue 2 years ago • 5 comments

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

bigbao9494 avatar Jun 03 '23 00:06 bigbao9494

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

PixlRainbow avatar Jun 03 '23 05:06 PixlRainbow

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()

}

bigbao9494 avatar Jun 05 '23 02:06 bigbao9494

Thanks for the good suggestions!

yhirose avatar Jun 05 '23 12:06 yhirose

@yhirose ,hi yhirose ,how about this featuer?

bigbao9494 avatar Jun 10 '23 10:06 bigbao9494

@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!

yhirose avatar Jul 08 '23 17:07 yhirose