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

proposal: net/http: Make parsePostForm public #63688

Open
mitar opened this issue Oct 23, 2023 · 4 comments
Open

proposal: net/http: Make parsePostForm public #63688

mitar opened this issue Oct 23, 2023 · 4 comments
Labels
Milestone

Comments

@mitar
Copy link
Contributor

mitar commented Oct 23, 2023

Currently, parsePostForm is private. It is called only from ParseForm and one cannot call manually. This means that if one wants to call it on a non-standard HTTP method (not POST, PUT, PATCH) you have to temporary change method, then call ParseForm and then return it back. Or if you want to parse just post form and ignore any query string parameters. Currently you cannot ignore string parameters (you have to set RawQueryString to "" currently). In my case, I want to parse query string and post form separately because I want o log parsed query string but not post form (the latter might have sensitive data).

I think code could be something like:

func (r *Request) ParsePostForm() error {
	var err error
	if r.PostForm == nil {
		r.PostForm, err = parsePostForm(r)
		if r.PostForm == nil {
			r.PostForm = make(url.Values)
		}
	}
	return err
}
func (r *Request) ParseForm() error {
	var err error
	if r.PostForm == nil {
		if r.Method == "POST" || r.Method == "PUT" || r.Method == "PATCH" {
			err = r.ParsePostForm()
		}
		if r.PostForm == nil {
			r.PostForm = make(url.Values)
		}
	}
	...
}
@mitar mitar added the Proposal label Oct 23, 2023
@gopherbot gopherbot added this to the Proposal milestone Oct 23, 2023
@seankhliao
Copy link
Member

Given what it does, why wouldn't you use url.ParseQuery directly?

@mitar
Copy link
Contributor Author

mitar commented Oct 23, 2023

parsePostForm parses application/x-www-form-urlencoded body, so it has a bit more logic than just calling url.ParseQuery.

@seankhliao
Copy link
Member

It only handles application/x-www-form-urlencoded, not multipart/form-data, and all it does is pretty much:

b, _ := io.ReadAll(r.Body)
v, _ := url.ParseQuery(string(b))

@mitar
Copy link
Contributor Author

mitar commented Oct 23, 2023

And sets limiter if it is not already set, etc. I understand I can make it myself. But why not make it public?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

3 participants