在GO中获得具有基本身份验证的http发布

I have a standard piece of go http request that I am trying to get right in the code below.

func IssueSearchQuery(conf Config) (string, error) {

    tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }

    var client = &http.Client{
        Timeout:   time.Second * 60,
        Transport: tr,
    }
    URL := conf.URL + "/api/search"
    v := url.Values{}
    v.Set("query_expression", "select * from test")
    req, err := http.NewRequest("POST", URL, 
        bytes.NewBufferString(v.Encode()))

    req.SetBasicAuth(conf.User, conf.Password)
    resp, err := client.Do(req)
    if err != nil {
        log.Fatal(err)
    }
    bodyText, err := ioutil.ReadAll(resp.Body)
    s := string(bodyText)
    return s, err
}

In the above code, I am connecting to the server, basic authentication works, but the server responds complaining saying the required query_expression query parameter is missing. If I do curl -u user:pass -d "query_expression=select * from test" --insecure -XPOST 'https://ip:port/api/search' the server responds with the required result.

The code also works if I don't use url.Values{} and instead manually encode my query into the url like URL := conf.URL + "/api/ariel/searches?query_expression=select%20*20from%20test"

I am not able to figure out what I am doing wrong with my query parameters. Can someone help? Thanks!

You appear to be trying to set the POST body to the url-encoded values. Either set your Content-Type header to application/x-www-form-urlencoded, and put them in the body as you're doing (thanks @hobbs), or use your url-encoded values in the actual url rather than the body:

u, err := url.Parse(conf.URL + "/api/search")
v := url.Values{}
v.Set("query_expression", "select * from test")
u.RawQuery = v.Encode()
req, err := http.NewRequest("POST", u.String(), nil)

Typically people expect POST data to be in the body, so just adding the req.Header.Set("Content-Type", "application/x-www-form-urlencoded") is probably the best.