When i make the POST request using the below GO Function. I get a invalid json on the server side.
If i send static json for example
var jsonprep = []byte(`{"username":"[email protected]","password":"xyz123"}`) it does work instead of
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)) } }
makeHttpPostReqfunction to accept aUserinstead of two strings.