This is a creation in Article, where the information may have evolved or changed.
Quick-cocos client program:
Local mainscene = Class ("Mainscene", function ()
Return Display.newscene ("Mainscene")
End
Local info={
hero={
Name= "Zhanshen", level=2,hp=500
},
pet={
Name= "Wind", hp=100,attack=200
}
}
function Mainscene:ctor ()
function Onrequestfinished (event)
Local OK = (Event.name = = "Completed")
Local request = Event.request
If not OK then
--Request failed with error code and error message displayed
Print (Request:geterrorcode (), Request:geterrormessage ())
Return
End
Local code = Request:getresponsestatuscode ()
If code ~=
--end of request, but no 200 response code returned
Print (code)
Return
End
--The request succeeds, showing the content returned by the server
Local Resdata=request:getresponsedata ()
Resdata=json.decode (Resdata)
Print (Resdata)
Dump (Resdata)
End
--Create a request and send the data to the server by POST
Local URL = "Http://10.12.15.70:8080/test"
Local request = Network.createhttprequest (onrequestfinished, URL, "POST")
--For k,v in pairs (info) do
--For k,v in pairs (v) do
--Request:addpostvalue (K,V)
--End
--End
Local Js=json.encode (info)
Request:setpostdata (JS)
--Start the request. The callback () function is called when the request is complete
Request:start ()
End
function Mainscene:onenter ()
End
function Mainscene:onexit ()
End
Return Mainscene
Golang Service-side program:
Package Main
Import (
"Encoding/json"
"FMT"
"Io/ioutil"
"Net/http"
)
var info map[string]string
Func Main () {
http. Handlefunc ("/test", hand)
ERR: = http. Listenandserve ("10.12.15.70:8080", nil)
If err! = Nil {
Fmt. Println ("Err")
}
}
Func Hand (w http. Responsewriter, R *http. Request) {
info = make (map[string]string)
info["name"] = "Server"
Info["level"] = "1"
Data, err: = json. Marshal (Info)
If err! = Nil {
Fmt. PRINTLN (ERR)
}
Fmt. Println ("OK")
W.write ([]byte ("Connect OK"))
W.write (data)
R.parseform ()//parse parameter, default is not resolved
Fmt. PRINTLN (R.form)//These are server-side printing information
Fmt. PRINTLN ("Path", R.url. Path)
Fmt. PRINTLN ("scheme", R.url. Scheme)
Fmt. Println (r.form["Url_long"])
For k, V: = Range R.form {
//Fmt. Println ("Key:", K)
//Fmt. The type of Println ("Val:", v)//v is []string
// }
Defer R.body.close ()
Body, err: = Ioutil. ReadAll (R.body)//Read the information returned by the server
If err! = Nil {
Fmt. PRINTLN ("Read err")
}
Fmt. Println (body)
Fmt. Println (String (body))
var tmp interface{}//decode JSON data in unknown format
Json. Unmarshal (body, &tmp)
Fmt. PRINTLN (TMP)
}
Remember to change the IP address to your own