-1

I want to get all the post form data and get the values as string, however using PostParams I am able to get all the post data, but the values are as array key: [value], how can I get all the form data as string values?

[Edit] I may not stated clearly, but I need to get the whole form as JSON, because I need to send the form data to another API which need the body as JSON data

form.html

<form>
    <input type='text' name='key' />
    <input type='text' name='key2' />
</form>

script.js

$.ajax( {
    url: '/post/action',
    type: 'POST',
    data: new FormData(this),
    processData: false,
    contentType: false
}).done(function(r) {
    console.log(r);
});

action.go

func PostAction(c echo.Context) error {
    form, _ := c.FormParams()
    b, _ := json.Marshal(form)
    log.Println(b) // this will print: key: [value], key2: [value]
    // I want to get value as string: key: value, key2: value

    // ..other function, return, etc
}

How can I get the form values as string? Or is there another function that does this?

Thank you very much in advance

Charas
  • 1,753
  • 4
  • 21
  • 53

1 Answers1

3

No need to JSON marshal the already decoded form, simply do:

form["key"][0]
form["key2"][0]

See the docs on c.FormParams and then click on the return type to see its documentation, and you'll notice that it's just the stdlib's url.Values type, which is defined as a map of string slices (i.e., map[string][]string).


If you need to convert a value of type url.Values to a "simple object", you can convert it to a map[string]string using the following code:

o := make(map[string]string, len(form))
for k, v := range form {
    // if you are certain v[0] is present
    o[k] = v[0]

    // if you are not sure v[0] is present, check first
    if len(v) > 0 {
        o[k] = v[0]
    }
}

data, err := json.Marshal(o)
if err != nil {
    return err
}

// ...
mkopriva
  • 35,176
  • 4
  • 57
  • 71
  • Yup, but I need to send the whole form data to my API, which I need the body as JSON (hence the JSON marshal), and it would be to troublesome to loop through all the data and get the [0] key then convert to JSON – Charas Jan 11 '23 at 10:55
  • @Charas what is so troublesome about a loop with a single line in its body? – mkopriva Jan 11 '23 at 13:49
  • because I am trying to use this function as a kind of "gateway" to a lot of dynamic forms with various different keys name, which I want to pass it as a JSON body to various different APIs – Charas Jan 11 '23 at 14:49
  • let's say I have 10 forms (each form has multiple inputs and all different dynamic key names), I want to pass the form data values to my APIs as JSON body, so that I don't have to repetitively re-write all key values – Charas Jan 11 '23 at 14:54
  • @Charas that's still a single loop with a single line :). Unless I'm missing something, looping over `url.Values` and using the iteration variables to construct a "flat" object, requires exactly one loop with one line in its body. – mkopriva Jan 11 '23 at 14:58
  • @Charas I have added an example of what I mean in case there's some misunderstanding. – mkopriva Jan 11 '23 at 15:03
  • Sorry I don't think I get what you mean, could you give an example of the codes to loop over the url.Values and construct it into "flat" object? – Charas Jan 11 '23 at 15:03
  • Oh you did, thanks, let me try to implement it in my code – Charas Jan 11 '23 at 15:04
  • @Charas I made a small mistake (forgot the index expression), make sure to modify your code accordingly, else it won't even compile. – mkopriva Jan 11 '23 at 15:08
  • 1
    Thank you very much sir, it works, you just made my day, have been stuck on this for 2 days, sorry I am quite new in Go. Thanks again man. – Charas Jan 11 '23 at 15:16