Home > Net >  How to check if a value is present in a POST form with Go's net/http?
How to check if a value is present in a POST form with Go's net/http?

Time:06-17

According to the documentation:

PostFormValue returns the first value for the named component of the POST, PATCH, or PUT request body. URL query parameters are ignored. PostFormValue calls ParseMultipartForm and ParseForm if necessary and ignores any errors returned by these functions. If key is not present, PostFormValue returns the empty string.

So the function Request.PostFormValue(key string) string returns an empty string when key does not exist in the POST body, but also when it exists and its value is empty.

How can I only check if the key is in the POST body, regardless of its value?

CodePudding user response:

Parse the form and then check to see if the key is set in the post form.

req.ParseForm()
hasKey := req.PostForm.Has("key")
  • Related