当我使用下面的 GO 函数发出 POST 请求时。我invalid json在服务器端得到一个。
例如,如果我发送静态 json
var jsonprep = []byte(`{"username":"xyz@gmail.com","password":"xyz123"}`)
它确实有效,而不是
var jsonprep string = "`{username:"+username+",password:"+password+"}`"
.
func makeHttpPostReq(url string, username string, password string){
client := http.Client{}
var jsonprep string = "`{username:"+username+",password:"+password+"}`"
var jsonStr = []byte(jsonprep)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
fmt.Println("Unable to reach the server.")
} else {
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("body=", string(body))
}
}
慕码人2483693
相关分类