Make HTTP Get request in iOS with params - ios

I want to make an HTTP GET request from my iOS client and place in the request params. I have written code that performs a POST request, where it was very easy to use setHTTPBody to place a NSData* object in the request. How is this done for a GET request?
Thanks

In a GET method the parameters are passed in the query string - so you suffix your URL with something like ?param1=value1&param2=value2&param3=value3. You may need to percent-encode your query string if it contains characters like space, & or =

Related

Alamofire GET request isn't working correctly

I am sending out a GET request like so.
let parameters: Parameters = [
"filter": ["fruit": "apple"]
]
Alamofire.request(urlStr, method: .get, parameters: parameters, encoding:URLEncoding.default).responseJSON { response in
print(response.request!)
}
However, the GET request doesn't work correctly. I believe the parameters are not correct at all. This is the URL request that is printed out using the following code print(response.request!)
https://api.example.com/fruits?apiKey=1&filter%5Bfruit%5D=apple
However, using postman I can send the correct request and get the correct response using the following URL request.
https://api.example.com/fruits?apiKey=1&filter={"fruit":"apple"}
I don't know how to fix this. I tried many encoding types, but none of it worked. Any tips or suggestions are appreciated
Edit: I do get a response from the server using alamofire, but it isn't correct data because the paramters are ignored. However, the response I get from postman is correct.
Morning Curt,
Since there is no published specification for how to encode collection types, the convention of appending [] to the key for array values (foo[]=1&foo[]=2), and appending the key surrounded by square brackets for nested dictionary values (foo[bar]=baz) [is used].
In my opinion you should really avoid passing array objects in query strings. You could change your request to something like:
apiKey=1&fruit=apple
Where does presence of field fruit itself indicates response should be filtered.
Moreover:
Instead of using GET, you should consider using POST or PUT and passing values via JSON, XML, or another well-defined format. This could require server side changes obviously.
If server side is out of your control, you should consider manually encoding these parameters instead.
Source
You can find here an example of manually encoding.
Happy coding!

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.
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.

How to pass parameters in Twilio's outbound calls?

I believe Twilio's outbound call could be HTTP POST request. Is there a way I can pass my custom POST body (json etc) when making outbound voice call request? I'm writing a generic call center where I would like to pass the conversation workflow when making outbound calls so that the code which receives the call knows how to run the conversation. I looked at the documentation (https://www.twilio.com/docs/api/twiml/twilio_request) and looks like we can only pass standard parameters (from, to etc). Thanks for any help.
I believe the only parameter you can customize is the Url Parameter.
Your JSON is pretty much a string (you might have to url encode it and also watch for the length), but you could put it in the query string of the Url Parameter.
?json=url_encoded_json

get GET request in Golang on fcgi

I run my scripts under Apache. I understand how I can create request, for example:
http.Get(url)
How I can get GET request? I really dont see this information in docs. Thanks in advance!
UPD
For example, i do GET or POST-request to my go script from another script. In PHP I'd just write $a=$_GET["param"]. How i can do that in Go? Sorry for bad english, by the way
Your handler is passed a Request. In that Request you find for example the parameters in the Form field as soon as you parsed it using ParseForm :
// Form contains the parsed form data, including both the URL
// field's query parameters and the POST or PUT form data.
// This field is only available after ParseForm is called.
// The HTTP client ignores Form and uses Body instead.
Form url.Values

Passing array of parameters through get in rails

How do I pass array of parameters through Get method in rails? Currently my URL loocs like this:
http://localhost:3000/jobs/1017/editing_job_suites/1017/editing_member_jobs/new?ids[]=1025&ids[]=1027
How can I pass the array with Get method but avoid ?ids[]=1025&ids[]=1027 part.
Request is being sent with javascript window.open method. Is there any workaround to send not ajax Post request.
You should stick to using a GET request if you are not changing the state of anything, and all you want to to access a read only value.
To send an array of ids to rails in a GET request simply name your variable with square brackets at the end.
//angular snippet
$http.(method:'GET',
...
params: {'channel_id':2, 'product_ids[]': productIds}
//where productIds is an array of integers
...
)
Do not concatenate your ids as a comma separated list, just pass them individually redundantly. So in the url it would look something like this:
?channel_id=2&product_ids[]=6900&product_ids[]=6901
url encoded it will actually be more like this:
?channel_id=2&product_ids%5B%5D=6900&product_ids%5B%5D=6901
Rails will separate this back out for you.
Parameters: {"channel_id"=>"2", "product_ids"=>["6900", "6901"]}
No, GET can only put variables on the url itself. If you want the URL to be shorter, you have to POST. That's a limitation feature of HTTP, not Rails.
I recently wanted to do this and found a workaround that is a little less complex, and so has some complexity limitations but may also be easier to implement. Can't speak to security, etc.
If you pass your array values as a string with a delimiter, e.g.
http://example.com/controller?job_ids=2342,2354,25245
Then you can take the result and parse it back to what you want:
job_ids = params[:job_ids].split(',')
Then do whatever:
job_ids.each do |job_id|
job = Job.find(job_id.to_i)
end
etc
#Homan answer is valid for using an external client (e.g curl or angular). Inside Rails test cases though, you should not use []. Here's an example:
get get_test_cases_url(**path_params), params: {case_ids: ["NON-9450", "NON-12345", "NON-9361"]}
This is using new format where get_test_cases is name of route and you pass to the method all params needed to construct the URL path. Query params are a separate hash.
FYI if I use [] like case_ids[], then instead of ["NON-9450", "NON-12345", "NON-9361"] I'm getting it parsed to [["NON-9450"], ["NON-12345"], ["NON-9361"]]

Resources