Menu

Should I use structs with URL-encoded form requests?

Reddit r/golang·u/161ForAChange·6 months ago
#qLlN1LAI
#structs#form#struct#create#photo
Reading 0:00
15s threshold

I am currently working on an API client for Splunk which takes x-www-form-urlencoded data. I like having the request data as structs, because it feels cleaner and can then be easily marshaled to JSON. Unfortunately there does not seem to be a function to easily convert any struct to form data.

My first idea was to create a function that takes any struct and then uses reflection to create the form with url.Values{}. However, that seems kinda pointless since I have to create the struct anyway and can set the parameters manually instead. Structs in this case feel useless, but I would still like to use them for clarity so it is easy to look up which fields are in every request.

What are your opinions on this? Should I just forego structs altogether and just set the values, or is there another way using structs that is not needlessly complicated?