Go net/http get JSON format data in body
Package Mainimport ("Encoding/json" "FMT" "Io/ioutil" "net/http") type autotaskrequest struct {RequestID string ' JSON: "Re Questid "' Clone Clonemodel ' JSON:" clone "' Push Pushmodel ' JSON: ' push ' '}type clonemodel struct {//todo//' Method ': s Tring ' JSON: "Ceph" ' RequestID string ' JSON: "RequestID" ' Callbackurl string ' JSON: "Callbackurl" '}type pushmodel struct { RequestID string ' JSON: ' RequestID ' ' Callbackurl string ' json: ' Callbackurl ' ' IP string ' json: ' Remoteip ' ' Port int ' JSON: "RemotePort" ' User string ' JSON: "User" '}func Test (w http. Responsewriter, R *http. Request) {//R.parseform () defer FMT. fprintf (W, "ok\n") fmt. Println ("Method:", R.method) body, err: = Ioutil. ReadAll (r.body) if err! = Nil {fmt. Printf ("Read body err,%v\n", err) return}println ("JSON:", string (body)) var a autotaskrequestif err = json. Unmarshal (body, &a); Err! = Nil {fmt. Printf ("Unmarshal err,%v\n", err) return}fmt. Printf ("%+v", a)}func main () {http. Handlefunc ("/test", test) http. Listenandserve (":8888 ", Nil)}
The parameters passed by the client are as follows:
{ "RequestID": "xxxxx", "Clone": { "Method": "Ceph", "Callbackurl": "XXX", "Remoteip": "192.168.2.1", "RemotePort": 8080, "User": "XXX", "Pass": "XXX", "Path": "XXX", "filename": "XXX" }, "Optimize": { "Callbackurl": "XXX", "Filter": { "K1":true, "K2":false }, "Trim":true, "Progressive":true, "Quality": 100, "Colornum": 256, "Gifoptlevel": 2, "Svgo":true, "Guetzli":false, "Css_rewrite":false, "Js_rewrite":false }, "Push": { "Method": "Ceph", "Callbackurl": "XXX", "Remoteip": "192.168.2.1", "RemotePort": 8080, "User": "XXX", "Pass": "XXX", "Path": "XXX", "filename": "XXX" }}
JSON passed parameters, there are no corresponding fields on the service side
Go net/http get JSON format data in body