how do i create a post request like this in golang

Issue

Hi guys a have a question about POST REQUEST

I have a some python code like this
data = {
"name": "Frank",
"age": 21,
"nationality": ["Britan"],
}

r = requests.post(‘somesite’, json=data)
How i can make a POST requst similar this on GOLANG, i tried use "nationality": ["Britan"]
but i have a some errors with []
i tried to use map[string]string but ofc its not working
May be i can use some structure to resolve my problem

Solution

You should use map[string]interface{} instead of map[string]string

As this link (https://go.dev/blog/maps):

map[KeyType]ValueType

where KeyType may be any type that is comparable, and ValueType may be any type at all, including another map!

Your body has both string and slice type, so ValueType is interface{} better than string.

Answered By – nohattee

Answer Checked By – David Marino (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.