Siesta iOS GET request with url parameters

2019-04-10 02:21发布

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条回答
放荡不羁爱自由
2楼-- · 2019-04-10 02:48

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.

查看更多
登录 后发表回答