0

I've seen the following example of how to use net/http PostForm to send a basic string map as a POST request:

How to send a POST request in Go?

But the data that I need to post is slightly more complicated as it has nested json / nested string maps. An example of the data I need to post:

{"MyAttributes" : {"AttributeOne" : "one", "AttributeTwo":"two"}}

Can net/url Values represent that kind of nested data and/or how do I pass this to net/http PostForm?

1 Answer 1

1

It's possible

package main

import (
    "encoding/json"
    "log"
    "net/http"
    "net/url"
)

type Attributes struct {
    AttributeOne string
    AttributeTwo string
}

func main() {
    attributes := Attributes{"one", "two"}

    data, err := json.Marshal(attributes)

    if err != nil {
        log.Fatal("bad", err)
    }

    values := url.Values{}
    values.Set("MyAttributes", string(data))

    resp, error := http.PostForm("localhost:2021", values)

    // use resp and error later
}

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.