Application/x-www-form-urlencoded to application/json

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

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

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.