You can use the net/http
package in Go to send a POST request with headers. Here's an example of how you can do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
)
func main() {
url := "https://www.example.com/api/v1/create"
contentType := "application/json"
data := []byte(`{"name": "Test User", "email": "test@example.com"}`)
client := &http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", contentType)
req.Header.Add("Authorization", "Bearer YOUR_ACCESS_TOKEN")
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
In this example, we create a new HTTP request with the POST
method, set the URL to https://www.example.com/api/v1/create
, and set the request body to a JSON payload. We also set the Content-Type
header to application/json
and add an Authorization
header with a bearer token. Finally, we use the http.Client
to send the request and print the response body.