我正在尝试从 golang 中的 http 请求中获取数据。我正在使用 net/http 包。在我的服务器处理程序中,我试图从 r.Body 获取数据
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("FATAL IO reader issue %s ", err.Error())
}
当我使用一些输入数据卷曲服务时,它工作正常。
curl --data '{"AppName":"Proline","Properties":null,"Object":"","Timestamp":"2016:03:27 00:08:11"}' -XGET http://localhost:8081/api/services/test/
但是当我尝试从 ajax call 调用这个服务时,r.Body 是空的。
requestJSON = '{"AppName":"Proline","Properties":null,"Object":"","Timestamp":"2016:03:27 00:08:11"}'
$.ajax({
type: "GET",
url: "http://localhost:8081/api/services/test/",
data: requestJSON,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){alert(data);},
failure: function(errMsg) {
alert(errMsg);
}
});
所以我改为从 r.Form
r.ParseForm()
var body []byte
for key, _ := range r.Form {
body = []byte(key)
break
}
但是现在 curl 请求失败了。有没有标准的方法可以从 golang 的 http 请求中检索输入数据?我正在使用 Go 1.6。有人可以帮我解决这个问题吗?
幕布斯6054654
相关分类