如何让REST端点接受使用相同密钥的多个查询参数?

时间:2017-01-30 12:17:42

标签: rest coldfusion coldfusion-11

我有一个简单的示例REST端点:

.htaccess

如果我从一个简单的HTML页面调用它:

component
  restpath = "checkboxes"
  rest     = true
{
  remote void function get(
    string value restargsource = "query" default=""
  )
    httpmethod  = "GET"
    restpath    = ""
    produces    = "application/json"
  {
    restSetResponse( {
      "status" = 200,
      "content" = SerializeJSON( ListToArray( value, ',' ) )
    } );
  }
}

然后HTTP GET请求具有如下的URI:

<html>
<body>
  <form action="/rest/checkboxes/" method="GET">
    <label><input name="value" type="checkbox" value="a" /> a</label><br />
    <label><input name="value" type="checkbox" value="b" /> b</label><br />
    <label><input name="value" type="checkbox" value="c" /> c</label><br />
    <label><input name="value" type="checkbox" value="d" /> d</label><br />
    <label><input name="value" type="checkbox" value="e" /> e</label><br />
    <button type="submit">Submit</button>
  </form>
</body>
</html>

如果有多个键值对具有相同的键。

但是,组件的输出只是:

http://localhost/rest/checkboxes/?value=b&value=e

省略相同键的第二个和任何后续值。

如何获取密钥的所有值?

1 个答案:

答案 0 :(得分:3)

使用["b"] 范围(带有重复键的参数表示为以逗号分隔的字符串):

URL

component restpath = "checkboxes" rest = true { remote void function checkboxes( string value restargsource = "query" default="" ) httpmethod = "GET" restpath = "" produces = "application/json" { var v = []; if ( StructKeyExists( URL, "value" ) ) { v = ListToArray( URL.value, ',' ); } restSetResponse( { "status" = 200, "content" = SerializeJson( v ) } ); } } (带有重复键的参数表示为字符串数组):

getPageContext().getRequest().getParameterMap()[key]