POST请求中的嵌套参数

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

POST Request with nested arguments

问题

我对Go还比较新,所以如果这是一个简单的问题,我很抱歉。

我目前正在尝试编写一个创建带有嵌套参数哈希的POST请求的函数。所以在像JavaScript这样的动态语言中,等效的调用将是:

$.post('http://example.com', {level1: {level2: 'foo'}});

在我的Go代码中,我目前将哈希嵌套在以下方式中:

func (runner *Runner) post(args... interface{}) interface{} {
    form_url := getString(args[0])
    form_data := ???

    http.PostForm(form_url, form_data)

表单数据的实际类型(interface{})由第三方库提供,所以我不能真正改变它。

问题是PostForm期望表单数据的类型为url.Values,其定义如下:

type Values map[string][]string

如何处理这个问题?到目前为止,我的结论是我需要编写一个函数来对嵌套哈希进行HTTP编码,并具有以下签名:

func httpEncodeNestedMap(data interface{}) map[string][]string {...}

在Go中,这个的惯用实现是什么?

谢谢。

英文:

I'm fairly new to Go, so I apologize if this is a trivial question.

I'm currently trying to write a function that creates a POST request with a nested parameter hash. So the equivalent call in a dynamic language such as javascript would be:

$.post('http://example.com', {level1: {level2: 'foo'}});

In my Go code, I currently have have the hash nested in the following way:

func (runner *Runner) post(args... interface{}) interface{} {
    form_url := getString(args[0])
    form_data := ???

    http.PostForm(form_url, form_data)

The actual type for the form data (interface{}) is provided by a 3rd party library, so I cannot really change it.

The problem is the PostForm expects a url.Values type for the form data, which is define as

type Values map[string][]string

What would be the best of handling this? My conclusion so far is that I would need to write a function that would HTTP encode the nested hash and have the following signature:

func httpEncodeNestedMap(data interface{}) map[string][]string {...}

What would be the idiomatic implementation of this in Go?

Thanks.

答案1

得分: 4

基本上,在HTTP中没有参数的“嵌套”概念。一些系统提供了解析查询字符串的功能,以模拟维度。

例如,PHP支持[]表示法。在PHP中,你可以有一个查询字符串,像foo[bar]=baz&foo[zar]=boo,它会被转换为一个$_REQUEST结构,如下所示:

 array(
     'foo' => array(
         'bar' => 'baz',
         'zar' => 'boo'
      )
 )

标准的Go库没有提供这样的功能,有其合理的原因。不过你可以自己实现它。

Go支持的是PHP不支持的(至少不是“本地”支持)查询参数的多个值。

type Values map[string][]string

注意,任何参数名(字符串索引)都可以有多个值(字符串切片)。当你的查询字符串看起来像foo=bar&foo=zar时,你会得到:

map[string][]string {
    "foo": {
        "bar",
        "zar"
    }
}

希望这能澄清问题。

英文:

Basically, in HTTP there is no 'nesting' of parameters per se. Some systems provide facilities to parse a query string in a way that simulates dimensions.

For example, PHP supports the []-notation. In PHP you can have a query string like foo[bar]=baz&foo[zar]=boo which would translate to a $_REQUEST structure like this:

 array(
     'foo' => array(
         'bar' => 'baz',
         'zar' => 'boo'
      )
 )

The standard Go library does not provide such functionality for valid reasons. You can implement it on your own though.

What Go does support and PHP does not (at least not "natively") is multiple values for query parameters.

type Values map[string][]string

Note how any parameter name (string index) can have multiple values (slice of strings). This occurrs when your query string looks like foo=bar&foo=zar, then you'd get:

map[string][]string {
    "foo": {
        "bar",
        "zar"
    }
}

I hope this clears things up.

huangapple
  • 本文由 发表于 2012年9月3日 06:23:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/12240238.html
匿名

发表评论

匿名网友

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

确定