I am trying to set the request header “content-type” to “application/json”.
But when I am submitting the form “content-type” is showing “application/x-www-form-urlencoded” inside request headers
Form tag inside html
<form action="/home" method="POST">
<input name="email" id="email" type="email" placeholder="Your Email">
<input name="password" id="password" type="password" placeholder="Your Password">
<input type="submit" >
</form>
I used inside my handler to set the content type
r.Header.Set("Content-Type", "application/json")
eudore
(Eudore)
January 3, 2021, 4:14pm
2
html form lable post data is form type, use ajax commit json data.
Actually what I am trying to do is want to store the form values inside my User model.
User Model
type User struct {
Email string `json:"email"`
Password string `json:"password"`
}
Inside my handler:-
func home(w http.ResponseWriter, r *http.Request) {
var user User
r.Header.Set("Content-Type", "application/json")
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil {
fmt.Println("Error in decoding", err)
}
fmt.Printf("Your username :- %v \n", user.Email)
fmt.Printf("Your password :- %v \n", user.Password)
tpl.ExecuteTemplate(w, "home.gohtml", user)
}
But When I am submitting the form I am getting this error message during decoding into user :-
invalid character 'e' looking for beginning of value
system
(system)
Closed
April 4, 2021, 5:56am
4
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.