Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
golang,从文件中读取内容,并在HTTP请求中作为正文数据发送_Http_Go_Filereader - Fatal编程技术网

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

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

然后,我将从服务器上得到一个错误,说“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("/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")