I found how to send a POST
with URL parameters, or how to send a POST
with a JSON body, but I do not know how to combine them together (a request with both a URL with parameters, and a JSON body).
The code below (which is not correct) shows the commbination I am looking for. I can use either bytes.NewBuffer(jsonStr)
or strings.NewReader(parm.Encode())
but not both.
package main
import (
"bytes"
"net/http"
"net/url"
"strings"
)
func main() {
var jsonStr = []byte(`{"title":"my request"}`)
parm := url.Values{}
parm.Add("token", "hello")
req, err := http.NewRequest("POST", "https://postman-echo.com/post", bytes.NewBuffer(jsonStr), strings.NewReader(parm.Encode()))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
}
How to build a full POST
call with all the components?
Use the only json as your request body, and apply the URL parameters like this:
req.URL.RawQuery = parm.Encode()