I am working on unit test for a restAPI implementation in Golang. I need to pass an array of object into url. Here is an example of struct I have:
type version struct {
Name string `json:"name"`
Ver string `json:"ver"`
}
type event struct {
ID string `json:"id"`
Title string `json:"Title"`
Description string `json:"Description"`
Versions []version `json:"versions"`
}
The sample json input i tested in postman will be look like this one
{
"id": "101",
"title": "This is simple Golang title for testing!",
"Description":"Sample code for REST api implementation in Golang 2021!",
"versions": [
{
"name": "pingPong",
"ver": "10.2"
},
{
"name": "Ninja",
"ver": "10.24"
}
]
}
My question is that how can i pass an array of objects as URL parameters. I expect to have something like below but not how to fill the ending part i highlighted by the ...
url?ID=20&Title=urlTitle&Description=UrlDescription&...
net/url.Valuesand encode the result. Keep in mind that the standard for URL query params does not specify how to encode nested structures, consequently the Go implementation doesn't support that. If you want to encode theVersionsslice into query params you'll have to choose a syntax and implement a solution, or look for one on the web.