Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Siesta iOS GET request with url parameters

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.

like image 496
o15a3d4l11s2 Avatar asked Dec 10 '22 16:12

o15a3d4l11s2


1 Answers

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.

like image 116
Paul Cantrell Avatar answered Dec 29 '22 00:12

Paul Cantrell