package main import ( "bytes" "encoding/json" "net/http" ) type Person struct { Name string Age int } func main() { person := Person{Name: "John", Age: 30} jsonData, _ := json.Marshal(person) req, _ := http.NewRequest("POST", "http://example.com/api/person", bytes.NewBuffer(jsonData)) req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, _ := client.Do(req) // handle response }
package main import ( "net/http" "net/url" ) func main() { formData := url.Values{ "username": {"john"}, "password": {"password123"}, } req, _ := http.NewRequest("POST", "http://example.com/login", bytes.NewBufferString(formData.Encode())) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") client := &http.Client{} resp, _ := client.Do(req) // handle response }In this example, we are sending form data in the request body to a remote server. We create a `url.Values` object with the form data, encode it to a string using `Encode`, and use it as the request body. We also set the `Content-Type` request header to `application/x-www-form-urlencoded`, which is the default content type for form data. The `net/http` package is a standard library package in Go.