1

I want to send this data "id":"ab1" , "name":"Mash" in a http request in Go.

example : --data-urlencode 'data=[{"id":"ab1"},{"name":"Mash"}]'

How can I send this in golang . I have string values to send id and name as seperate json objects in an array like [{"id":"ab1"},{"name":"Mash"}]

1 Answer 1

2

Use []interface{} to represent the data to be encoded to the JSON array. An interface{} can hold any Go value.

data := []interface{}{t1{ID: "123456"}, t2{Name: "Slash"}}
p, err := json.Marshal(data)
if err != nil {
    log.Fatal(err)
}

The types t1 and t2 in the snippet above are assumed to be the your types. Modify the names to your actual types.

Create a form:

form := url.Values{"data": []string{string(p)}}

Post the form:

http.DefaultClient.PostForm(url, form)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.