func SendClaim(w http.ResponseWriter, r *http.Request) {
// strEcho := "Halo"
servAddr := tcpAddress
type Resp struct {
Status string `json:"status"`
Reply string `json:"reply"`
}
type Claim struct {
ClaimInfo string `json:"claiminfo"`
}
var params Claim
erx := json.NewDecoder(r.Body).Decode(¶ms)
if erx != nil {
http.Error(w, erx.Error(), http.StatusBadRequest)
fmt.Println("Error occurs here")
log.Printf(erx.Error())
return
}
fmt.Printf("Claims: %s \n", params)
// policy := params.CardNo //r.FormValue("cardno")
// log.Println("policy # ", policy)
tcpAddr, err := net.ResolveTCPAddr("tcp", servAddr)
if err != nil {
println("ResolveTCPAddr failed:", err.Error())
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
data := params.ClaimInfo
// printr := r.FormValue("printer")
fmt.Println(data)
conn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
println("Dial failed:", err.Error())
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
_, err = conn.Write([]byte(data))
if err != nil {
println("Write to server failed:", err.Error())
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
reply := make([]byte, 1024)
_, err = conn.Read(reply)
if err != nil {
println("Write to server failed:", err.Error())
respondWithError(w, http.StatusBadRequest, err.Error())
return
}
resp := Resp{}
println("reply from server=", string(reply))
resp.Status = "response"
resp.Reply = string(reply)
respondWithJSON(w, http.StatusOK, resp)
conn.Close()
}