import ( "bytes" "encoding/json" "net/http" ) type User struct { Name string Age int } func main() { user := User{Name: "John", Age: 28} payload, _ := json.Marshal(user) req, _ := http.NewRequest("POST", "http://example.com/api/user", bytes.NewReader(payload)) req.Header.Set("Content-Type", "application/json") client := http.DefaultClient res, err := client.Do(req) if err != nil { // handle error } defer res.Body.Close() // handle response }
import ( "net/http" "net/url" ) func main() { values := url.Values{} values.Set("username", "john") values.Set("password", "mypassword") req, _ := http.NewRequest("POST", "http://example.com/api/login", bytes.NewBufferString(values.Encode())) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") client := http.DefaultClient res, err := client.Do(req) if err != nil { // handle error } defer res.Body.Close() // handle response }In this example, we create a url.Values object with the form data we want to send. We then create a new HTTP POST request with the form data as the request body. We also set the Content-Type header to indicate that the payload is in x-www-form-urlencoded format. Finally, we send the request using the default HTTP client and handle the response.