golang,从文件中读取内容,并在HTTP请求中作为正文数据发送
然后,我将从服务器上得到一个错误,说“400个错误请求”,“无效字符'ï'寻找值的开头”“ 数据似乎没有被正确解码 而下面的代码可以工作golang,从文件中读取内容,并在HTTP请求中作为正文数据发送,http,go,filereader,Http,Go,Filereader,然后,我将从服务器上得到一个错误,说“400个错误请求”,“无效字符'ï'寻找值的开头”“ 数据似乎没有被正确解码 而下面的代码可以工作 //read data from a file config.json //the contents of config.json is //{"key1":"...Z-DAaFpGT0t...","key2":"..."} client := &http.Client{} dat, err := ioutil.ReadFile("/confi
//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)
请检查所需的
内容类型
服务器,并将主体构建为内容类型
所需
因为您的
内容类型
是应用程序/json;charset=utf-8
,您尝试手动设置它
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)
如果仍然不起作用,请进行http负载捕获。是否检查这些错误?能否共享/config.json的内容?服务器需要什么
内容类型?应用程序/json;字符集=utf-8
req.Header.Set("Content-Type", "application/json; charset=utf-8")