go - Golang: Send parameters by POST -


resource receives parameters

example: http://example.com/show-data?json={"fob":"bar"}

in case of request, clear , work well.

urlstr := "http://87.236.22.7:1488/test/show-data" json := `"foo":"bar"` r, _ := http.get(urlstr+`?json=`+json) println(r.status)   200 ok 

but how shoud done when use post request?

i try

 urlstr := "http://87.236.22.7:1488/test/show-data"     json := `{"foo":"bar"}`     form := url.values{}     form.set("json", json)      println(form.encode())     post, _ := http.postform(urlstr, form)      println(post.status)    400 bad request   json parameter missing 

but it`s not work.

there bunch of ways post stuff, want use postform: https://golang.org/pkg/net/http/#postform

you'll need set values object first pass right in. see example code in docs: https://golang.org/pkg/net/url/#values

once you've jammed json values object, call postform() fire up.

edit: works assuming receiving end wanting encoded application/x-www-form-urlencoded. i'm putting in second answer if receiving end expecting application/json.


Comments