Golang使用PostParams获取所有POST表单数据,并将值作为字符串获取

huangapple go评论102阅读模式
英文:

Golang Get all POST Form data using PostParams and get values as string

问题

我想获取所有的表单数据并将其作为字符串获取值,但是使用PostParams我能够获取所有的表单数据,但是值是作为数组键:[值],我如何将所有的表单数据作为字符串值获取?

[编辑]
我可能没有表达清楚,但我需要将整个表单作为JSON获取,因为我需要将表单数据发送到另一个需要JSON数据作为请求体的API。

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) // 这将打印:key: [value], key2: [value]
    // 我想要将值作为字符串获取:key: value, key2: value

    // ..其他函数、返回等

}

我如何将表单值作为字符串获取?
或者是否有另一个函数可以实现这个功能?

非常感谢您的帮助。

英文:

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

&lt;form&gt;
    &lt;input type=&#39;text&#39; name=&#39;key&#39; /&gt;
    &lt;input type=&#39;text&#39; name=&#39;key2&#39; /&gt;
&lt;/form&gt;

script.js

$.ajax( {
    url: &#39;/post/action&#39;,
    type: &#39;POST&#39;,
    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

答案1

得分: 3

不需要对已解码的表单进行JSON编组,只需执行以下操作:

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

请参阅关于c.FormParams的文档,然后点击返回类型以查看其文档,您会注意到它只是标准库的url.Values类型,它被定义为字符串切片的映射(即map[string][]string)。


如果您需要将url.Values类型的值转换为“简单对象”,可以使用以下代码将其转换为map[string]string

o := make(map[string]string, len(form))
for k, v := range form {
    // 如果您确定v[0]存在
    o[k] = v[0]

    // 如果您不确定v[0]是否存在,请先进行检查
    if len(v) > 0 {
        o[k] = v[0]
    }
}

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

// ...
英文:

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

form[&quot;key&quot;][0]
form[&quot;key2&quot;][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) &gt; 0 {
        o[k] = v[0]
    }
}

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

// ...

huangapple
  • 本文由 发表于 2023年1月10日 23:12:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75071992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定