Reputation: 33715
I come from a PHP background and I'm trying to learn Go.
In the past, I can run this command from bash:
curl -X POST http://192.168.12.107/restfulendpoint.php \
-H "Content-Type: application/x-www-form-urlencoded" \
-d \
"menu[0][name]=pizza&menu[0][price]=12.00&menu[1][name]=orange juice&menu[1][price]=1.00"
My restfulendpoint.php
will naturally receive a POST
array that is structured in a nested format like so
Array
(
[menu] => Array
(
[0] => Array
(
[name] => pizza
[price] => 12.00
)
[1] => Array
(
[name] => orange juice
[price] => 1.00
)
)
)
I then created a go script that I hope would produce a similar behaviour. I wrote this restfulendpoint.go
func Post(w http.ResponseWriter, r *http.Request) {
url := mux.Vars(r)
r.ParseForm()
qs := r.Form
log.Println(qs)
}
But when I receive the POST content, I get a structure like this:
map[menu[0][price]:[12.00] menu[1][name]:[orange juice] menu[1][price]:[1.00] menu[0][name]:[pizza]]
Go is treating each map[<int>][price|name]
as a separate key in a map. What I really want is an output like this:
map[menu:[map[name:pizza price:12.00] map[name:pizza price:12.00]]]
Is there a convention I should follow in my curl
call to my restfulendpoint.go
so that I can naturally receive the data in a schema were each menu item has a name and price property? Or is there something I should do in my go script to preserve the data schema?
Upvotes: 1
Views: 1281
Reputation: 1779
It looks like go's ParseForm
doesn't cater for the format you use to encode hierarchical structure. I think you're faced with either reading the body as raw, then parsing it yourself, or, if you have the option to change the post format, simply sending the body as JSON instead of a form.
Upvotes: 2