3

I have two related questions, I did not want to open a new thread for both questions:

Given the following code:

1    type Request struct {
2       Values map[string]interface{}
3    }
4
5    func (r Request) Send() {
6       client := &http.Client{}
7       resp, _ := http.Post("http://example.com", "text/json", &r.Values)
8    }

The idea is to be able to send a block of an unknown amount key => value, key => key => value, etc. to our API endpoint.

Question 1:

How do I assign to Request.Values? An example use case we may need to employ is the following (Excuse the PHP Code, we're transitioning):

'name' => [ $first, $last ],
'address' => [ 'city' => 'city', 'state' => 'state' ],
'country' => 'US'

In this example we have key => value, key => [ values ], and key => [ key => value ]

How can I take that and assign the exact same values to Request.Values?

Question 2:

Obviously Values is of type map[string]interface{} , how can I convert that to type io.Reader so I can send the values to the server?

Any guidance is greatly appreciated on both questions.

1 Answer 1

6

Question 1

You can assign values to Request.Values just like with any other value.

Example (on play):

x := map[string]interface{}{
    "foo": []string{"a","b"},
    "bar": "foo",
    "baz": 10.4,
}

Question 2

You are in need of an intermediate format, for example JSON.

With this package you can marshal your Request struct to a JSON string, transfer said string and parse it on the other (PHP?) side.

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.