4

Is there a way to make a GET request in Siesta, while providing parameter, like http://example.com/api/list.json?myparam=1?

I tried with

myAPI.resource("list.json?myparam=1")

but the question mark gets escaped.

Then I tried with

myAPI.resource("list.json").request(.GET, urlEncoded:["myparam": "1"])

but it always fails with "The network connection was lost.", but all other requests succeed, so the message is wrong.

4

1 回答 1

13

您正在寻找withParam

myAPI.resource("list.json").withParam("myparam", "1")

Service.resource(_:)您在第一个示例中尝试使用的方法专门避免将特殊字符解释为参数(或路径以外的任何内容)。从文档:

path 参数只是简单地附加到 baseURL 的路径,并且永远不会被解释为 URL。..、//、?、https等字符串:没有特殊含义;它们直接进入结果资源的路径,必要时转义。

这是一项安全功能,旨在防止用户提交的字符串渗入 URL 的其他部分。

第二个示例中的Resource.request(_:urlEncoded:)方法是用于传递请求正文中的参数(即使用 POST 或 PUT),而不是用于查询字符串中的参数。

请注意,如果您想绕过 Siesta 的 URL 组件隔离和转义功能,您始终可以使用Service.resource(absoluteURL:)自己构造 URL。

于 2016-05-17T14:29:30.760 回答