带有url参数的Siesta iOS GET请求 [英] Siesta iOS GET request with url parameters
问题描述
有没有办法在Siesta中提供GET请求,同时提供参数,例如 http ://example.com/api/list.json?myparam = 1 ?
Is there a way to make a GET request in Siesta, while providing parameter, like http://example.com/api/list.json?myparam=1?
我试过
myAPI.resource("list.json?myparam=1")
但问号被转义。
然后我尝试了
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.
推荐答案
您正在寻找 withParam
:
You are looking for withParam
:
myAPI.resource("list.json").withParam("myparam", "1")
您尝试在第一个示例中使用的Service.resource(_:)
方法特别避免将特殊字符解释为params(或除路径之外的任何内容)。来自文档:
The Service.resource(_:)
method you are trying to use in your first example specifically avoids interpreting special characters as params (or anything except a path). From the docs:
path参数只是附加到baseURL的路径,永远不会被解释为URL。诸如..,//,?和https之类的字符串没有特殊含义;它们会直接进入生成的资源路径,必要时可以转义。
The path parameter is simply appended to baseURL’s path, and is never interpreted as a URL. Strings such as .., //, ?, and https: have no special meaning; they go directly into the resulting resource’s path, with escaping if necessary.
这是一个安全功能,用于防止用户提交的字符串从流血到URL的其他部分。
This is a security feature, meant to prevent user-submitted strings from bleeding into other parts of the URL.
您的第二个示例中的Resource.request(_:urlEncoded:)
方法用于在请求正文中传递参数(即使用POST或PUT),而不是查询字符串中的参数。
The Resource.request(_:urlEncoded:)
method in your second example is for passing parameters in a request body (i.e. with a POST or PUT), not for parameters in the query string.
请注意,您始终可以使用 Service.resource(absoluteURL :) 如果你想绕过Siesta的URL co,你自己构建一个URL mponent隔离和转义功能。
Note that you can always use Service.resource(absoluteURL:) to construct a URL yourself if you want to bypass Siesta’s URL component isolation and escaping features.
这篇关于带有url参数的Siesta iOS GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!