Siesta iOS GET request with url parameters

扶醉桌前 提交于 2019-12-06 06:14:30

问题


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.


回答1:


You are looking for withParam:

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

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:

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.

This is a security feature, meant to prevent user-submitted strings from bleeding into other parts of the URL.

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.

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.



来源:https://stackoverflow.com/questions/37276714/siesta-ios-get-request-with-url-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!