How to Send Form Data (application/x-www-form-urlencoded) in Go

Web
Encode form data using url.Values and set the Content-Type header to application/x-www-form-urlencoded in Go.

Use url.Values to encode your data and set the Content-Type header to application/x-www-form-urlencoded before sending the request.

import (
	"net/http"
	"net/url"
)

values := url.Values{
	"field1": []string{"value1"},
	"field2": []string{"value2"},
}

req, _ := http.NewRequest("POST", "https://example.com/api", strings.NewReader(values.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

resp, err := http.DefaultClient.Do(req)