I am using Alamofire and I want to send a get request. If this fails, I want to retry it again after some other operations.
I save the request in a variable:
let myRequest = Alamofire.request("myurl", method: .get)
and in another function:
func retry(request:DataRequest) {
request.responseSwiftyJSON { response in
// ... code to handle the response ...
}
}
In this way, I can call multiple times the retry function, passing the same object myRequest.
However, the request is sent correctly only the first time, then I think it is cached (or in some way the Alamofire object realizes that the response field of the request object is already valid) and the request is not sent again to the server.
To solve this problem, I tried to change a little bit the retry function in this way:
func retry2(request:DataRequest) {
Alamofire.request(request.request!).responseSwiftyJSON { response in
// ... code to handle the response ...
}
}
This should initialize a new Alamofire request using only the URLRequest field of the saved request, but now the request is called twice every time! (I check it in my backend and I am sure this is caused by using this approach).
Is there any way to resend the original saved request? Does Alamofire have some way to initialize a new request from a saved one?
Solution
I ended up by doing something like #JonShier and #DuncanC suggested me:
struct APIRequest {
let url: URLConvertible
let method: HTTPMethod
let parameters: Parameters?
let encoding: ParameterEncoding
let headers: HTTPHeaders?
init(_ url:URLConvertible, method:HTTPMethod = .get, parameters:Parameters? = nil, encoding:ParameterEncoding = URLEncoding.default, headers:HTTPHeaders? = nil) {
self.url = url
self.method = method
self.parameters = parameters
self.encoding = encoding
self.headers = headers
}
func make() -> DataRequest {
return Alamofire.request(url, method: method, parameters: parameters, encoding: encoding, headers: headers)
}
}
I haven't used AlamoFire much, and that minimal use was a couple of years ago.
Looking around in the AlamoFire code, it appears that the request() function returns a DataRequest object. Further, it looks like a DataRequest is a reference type that is meant to track a specific request to a server and it's responses.
It looks to make like once you create one, you use it until it completes or fails, and then discard it. They do not appear to be intended to be reused for subsequent requests of the same type.
To summarize:
Question: "How do I reuse AlamoFire DataRequest objects."
Answer: "Don't do that."
Related
We are facing the following networking error when the response is somehow large(14kb) on iOS 13.
[-1103] Error Domain=NSURLErrorDomain Code=-1103 "resource exceeds maximum size"
As we are using Alamofire, this problem is treated as error result which breaks our treatments of the results.
The strange thing is that if we use NSURLSession directly, though this error is still seen from logging, we don't actually receive it in the callback of
session.dataTask(with: request) { value, response, error in ... }
So the result can treated correctly.
This problem is never seen before. Anyone has got some idea on that ?
With the help of the Slack community, we find the answer is that
on iOS13, it is not allowed to add a body in GET request. To make it work again, we can either switch to a POST/PUT request or add body value via url parameters of the GET request.
Pass query parameters in GET request like the following:
let parameters: Parameters = [
"param": value
]
Alamofire.request(urlString, method: .get, parameters: parameters, encoding: URLEncoding.queryString)
I have face same issue and find out the solution.
You can't pass parameter in body while using GET.
Either use POST method if API support or pass it in URL like below.
AnyURL?Parameter=Value&Parameter=Value
Finally found the answer. For GET services I was trying to add an httpBody. Something like this:
do {
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
} catch let error {
errorCompletion(error)
return
}
The solution was just to add an if to avoid that chunk of code if httpMethod is a GET. Seems like an iOS 13 new behavior and the error message given by Swift definitely not helps at all
Alamofire: You should try this!
Alamofire.request(urlString, method: .get, parameters: parameters, encoding: URLEncoding.queryString)
Just avoid the httpBody for the GET API request.
if requestType != .get{
request.httpBody = data
}
#OR
For GET request append parameter into URL instead of the HTTP body
Use the below extension to create a query parameter from the dictionary.
extension NSObject {
func buildQueryString(fromDictionary parameters: [String:String]) -> String {
var urlVars = [String]()
for (var k, var v) in parameters {
let characters = (CharacterSet.urlQueryAllowed as NSCharacterSet).mutableCopy() as! NSMutableCharacterSet
characters.removeCharacters(in: "&")
v = v.addingPercentEncoding(withAllowedCharacters: characters as CharacterSet)!
k = k.addingPercentEncoding(withAllowedCharacters: characters as CharacterSet)!
urlVars += [k + "=" + "\(v)"]
}
return (!urlVars.isEmpty ? "?" : "") + urlVars.joined(separator: "&")
}
}
I used default url encoding instead of default json encoding and it's worked for me.
Alamofire.request(url, method: .get, parameters: param, encoding: URLEncoding.default)
OR
If you using URLRequestConvertible
enum NetworkRouter: URLRequestConvertible {
case someCase(lang:String)
var method: HTTPMethod {
return .get
}
var parameters: Parameters? {
switch self {
case .someCase(let param):
return ["lang": param.lang]
default:
return nil
}
}
var url: URL {
switch self {
case .someCase(let param):
return URL(string: Constants.baseURL + Constants.endPoint)!
default:
return URL(string: Constants.baseURL)!
}
}
var encoding: ParameterEncoding {
return URLEncoding.default
}
func asURLRequest() throws -> URLRequest {
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = method.rawValue
return try encoding.encode(urlRequest, with: parameters)
}
}
I got that issue because i pass empty parameters in Alamofire when send get request. So, instead of sending empty parameters i simply replace it for nil.
My fix is I only set .parameters to nil, then everything works fine. Because in Swift it still initialize the value of .parameters.
self.request.parameters = nil
Here you might have missing the method of the URL request that you are passing to data task. You have to add POST/PUT/DELETE to the URL request method parameter like below.
var request: URLRequest = URLRequest(url: SOME_VALID_URL)
request.body = SOME_VALID_DATA_IN_BYTES
request.method = "post" --> You are missing this.
I only see this issue when I build with Xcode 11. If I revert back to Xcode 10.3 I do not have the same issue anymore. While not a forever fix, if you're needing to push out code you can revert until you have time to fix it.
I am new to iOS and want to call an API for login. I am using Alamofire for HTTP request. I am getting a response from the server but I guess it is saving response in cache. So if I call api with different data is printing same response again. And one more question is I want to save session for later api call. How can I save session data and use it in header for later api call?
This is my Login API Call
let params = ["identity": txtId.text!, "pass": txtPassword.text!]
AF.request(LOGIN_URL, method: .post, parameters: params as Parameters).responseJSON { response in
print("response = \(response.result.value)")
}
One easy way to remove caches is to call this code before your network call:
URLCache.shared.removeAllCachedResponses()
Or you can also remove caches for a single request:
let urlRequest = URLRequest(url: URL(string: "https://...")!)
URLCache.shared.removeCachedResponse(for: urlRequest)
Then, make your network call:
let params = ["identity": txtId.text!, "pass": txtPassword.text!]
AF.request(LOGIN_URL, method: .post, parameters: params as Parameters).responseJSON { response in
print("response = \(response.result.value)")
}
How can I use Alamofire networking framework to add Authorization header to each request, and also do a check whether the response was 401 from the server, so that I can do logic accordingly, and also present some kind of a login view.
Would I need to create some kind of an HttpService class that wraps Alamofire requests? Or is there a more built in way?
As per to your requirement, I would personally prefer to have a intermediate class for calling methods of Alamofire.
For that you can add auth header on each web service call.
Here is the following example for Intermediate class.
WebClient.swift
class WebClient: SessionManager {
static let sharedManager = WebClient()
func responseRequest(_ url: String, method: Alamofire.HTTPMethod, parameter: Parameters? = nil, encoding: ParameterEncoding, header: HTTPHeaders? = nil, completionHandler: #escaping (DefaultDataResponse) -> Void) -> Void {
self.request(url, method: method, parameters: parameter, encoding: encoding, headers: header).response { response in
completionHandler(response)
}
} }
You can modify above class as per requirement or you can directly pass header in request method for each webservice call.
Adding authorization headers for HTTP Basic Auth and checking the status code is fairly easy:
Alamofire
.request(...)
.authenticate(user: "username", password: "password")
.response { response in
if let status = response.response?.statusCode, status == 401 {
// got a 401 response
}
}
I am using Alamofire library for REST API calls. I have a Request Body of type
Dictionary(String, Any). There are few objects in the Request Body that are common for some APIs.
For eg: 1st API call contains following parameters in the request body.
var hotel = HotelParameter()
var food = foodParameter()
var address = addressParameter()
class RequestParameters: NSObject{
func parameter() -> NSDictionary {
var parameter : [String : Any] = [:]
parameter["hotels"] = hotel.params()
parameter["foodType"] = food.params()
parameter["address"] = address.params()
return parameter as! NSDictionary
}
Now in 2nd API call I have to pass only "hotels" and "address" in the RequestParameter.
My problem is: How to add or remove any extra parameter in the Alamofire Request Body?
Since I cannot add Request Body for 'n' number of Requests, there has to be a single RequestParameter which will get modified according to the api calls. There can be extra or single Parameter(s) in the Body.
Mostly "hotels" will be a common parameter for all Request Body.
I have tried a lot to solve this, But I get exception because it cannot parse some JSON response.
I have created functions to hit the api calls and I am passing the parameters like this.
var requestParam = RequestParamters()
Alamofire.request(url,
method: .post,
parameters: requestParam.parameter(),
encoding: JSONEncoding.default, headers: headers)
.responseJSON(completionHandler: { (data : DataResponse<Any>) in
I am not sure, I got your requirement right. But as per my understanding, you have to hit one API multiple times, and the parameter shall be different every time. So just write a method which'll take parameter as a method argument. Here is the sample code as per your requirement.
func getAllHotels(_ params:Dictionary, callback:#escaping (Dictionary<String, Any>, Error?)->Void)
{
let parameters: Parameters = params
Alamofire.request(baseUrl + "/getHotels", method: .post,parameters: parameters, encoding: URLEncoding.default).responseJSON { response in
debugPrint("All Response from API: \(response)")
switch response.result
{
case .success(let value):
callback(value as! Dictionary, nil)
case .failure(let error):
callback([:], error)
}
}
}
/** Now call the above method with your required parameters **/
I found out Two ways to do it.
Create an addExtraParams :[String : Any] = : type of Dictionary and add the key : value pair you need in this.
Create Requests functions for respective apis and just update the value of the required parameters using the object you get after logging.
For eg:
var user = response.object as! Person
var request = RequestParameters()
request.hotel.name = user.hotel_name
Update the values before the Alamofire request in the function that you have created.
I have a method which handles a Apple Push Notification Service remote notification. When this method is executed, I want it to call my server and do a HTTP POST request using the Alamofire library. I want to execute another method that will handle the response of the POST request.
The problem for me is that I am using an existing API to fetch a profile from the server in this POST request. So I need to use this existing API and figure out when this profile fetch is specifically triggered from the remote notification.
Since Alamofire requests are done in a background queue, how would I go about doing an execution of a method after receiving the profile back from the server?
What would be a good option to solving this issue?
Thank you!
Since Alamofire requests are done in a background queue, how would I go about doing an execution of a method after receiving the profile back from the server?
Response handling is built in to Alamofire. You can do something like this (adapted from the docs):
Alamofire.request(.POST, "http://httpbin.org/get", parameters: ["foo": "bar"])
.response { (request, response, data, error) in
println(request)
println(response)
println(error)
}
Note the .response method call, which adds a completion handler to the request object; the completion handler is invoked by Alamofire when the request completes (or fails).
It wasn't clear from your question formulation what problem you were trying to solve. But you've clarified your intent in the question comments above.
As I understand the problem now, you're got some code that updates a profile on the server and handles the server's response. The code is called in two contexts, one initiated by a manual request from the user, another initiated by a push notification. In the first case, you don't want to generate an alert after you process the response from the server, but in the second case you do.
You do indeed have a closure that you can use to handle the different behavior even though the difference happens in the asynchronous part of the process. Here's a sketch (not actual working code) of how that might look:
func updateProfile(parameters: [String:String], showAlert: Bool) {
Alamofire.request(.POST, "http://myserver.com/profile", parameters: parameters)
.response { (request, response, data, error) in
if (error == nil) {
processProfileResponse(response)
if showAlert {
showProfileWasUpdatedAlert()
}
}
}
}
Note the showAlert parameter passed in to the updateProfile method. If you pass in true, it calls the showProfileWasUpdatedAlert method to show your alert after receiving the server's response. Note that this boolean value is "captured" by the closure that handles the Alamofire response because the closure was defined inside the updateProfile function.
This, IMHO, is a better approach than declaring an app global inside your AppDelegate.
Here you go
func AlamofireRequest(method: Alamofire.Method, URLString: URLStringConvertible, parameters: [String : AnyObject]?, encoding: ParameterEncoding, headers: [String : String]?) -> Alamofire.Result<String>? {
var finishFlag = 0
var AlamofireResult: Alamofire.Result<String>? = nil
Alamofire.request(method, URLString, parameters: parameters, encoding: encoding, headers: headers)
.responseString { (_, _, result) -> Void in
if result.isSuccess {
finishFlag = 1
AlamofireResult = result
}
else {
finishFlag = -1
}
}
while finishFlag == 0 {
NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate.distantFuture())
}
return AlamofireResult
}