golang,从文件中读取内容,并在HTTP请求中作为正文数据发送

//read data from a file config.json
//the contents of config.json is
//{"key1":"...Z-DAaFpGT0t...","key2":"..."}
 client :=   &http.Client{}
 dat, err := ioutil.ReadFile("/config.json")
 req, err := http.NewRequest("PUT", url, bytes.NewBuffer(dat))
 resp, err := client.Do(req)

Then I will get error from server saying "400 Bad Request", " "invalid character 'ï' looking for beginning of value"".

It seems the data is not properly decoded.

While code below works

 client :=   &http.Client{}
 //dat, err := ioutil.ReadFile("/config.json")
 var dat = []byte(`{"key1":"...Z-DAaFpGT0t...","key2":"..."}`)
 req, err := http.NewRequest("PUT", url, bytes.NewBuffer(dat))
 resp, err := client.Do(req)

please check content-type your server required, and build the body as content-type required.

as your content-type is application/json; charset=utf-8, you try to set it manully

req.Header.Set("Content-Type", "application/json; charset=utf-8")

if it still not work, make a http load capture please.