Alamofire response object returns status code 200 when my Rails server returns 304

When I send a request to my rails server and do not receive 304, it does not return the status status of the response to the almofire object. How can I modify my request to get the status code 304 that my rails server returns? I installed Alamofire using cocoapods.

EDIT

This is my code currently (not working):

if Reachability.isConnectedToNetwork() { let urlreq = NSMutableURLRequest(URL: NSURL(string: API.feedURL())!,cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData , timeoutInterval: 5000) Alamofire.request(.GET, urlreq, parameters: ["category_name":category],encoding: ParameterEncoding.URL, headers: API.getHeaders()) .validate(statusCode: 200..<500) .responseJSON { request, response, result in switch result { case .Success(let data): let statusCode = response!.statusCode as Int! if statusCode == 304 { completionHandler(didModified: false, battlesArray: []) } else if statusCode == 200 { let json = JSON(data) var battlesArray : [Battle] = [] for (_,subJson):(String, JSON) in json["battles"] { battlesArray.append(Battle(json: subJson)) } completionHandler(didModified: true, battlesArray: battlesArray) } case .Failure(_, let error): print ("error with conneciton:\(error)") } SVProgressHUD.dismiss() } else { //nothing important } 

This is the code after kpsharp's answer (doesn't work):

 if Reachability.isConnectedToNetwork() { let urlreq = NSMutableURLRequest(URL: NSURL(string: API.feedURL()+"?category_name=Popular")!,cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData , timeoutInterval: 5000) urlreq.HTTPMethod = "GET" let headers = API.getHeaders() for (key,value) in headers { urlreq.setValue(value, forHTTPHeaderField: key) } urlreq.cachePolicy = .ReloadIgnoringLocalAndRemoteCacheData Alamofire.request(urlreq) } else { //nothing interesting } 

EDIT 2

My caching rails code is:

 def index battles = Battle.feed(current_user, params[:category_name], params[:future_time]) @battles = paginate battles, per_page: 50 if stale?([@battles, current_user.id], template: false) render 'index' end end 

thanks

+8
ios ruby-on-rails swift alamofire
source share
1 answer

This is already a known issue at Alamofire .

cnoon , a member of Alamofire, recommended the following:

Great question ... quite possibly already. You need to use URLRequestConvertible in combination with NSMutableURLRequest to override cachePolicy for this particular request. Check the documentation and you will see what I mean.

EDIT: In response to your comment, I will provide some quick codes that hopefully take things away.

So the problem is that you have a cached answer. In most cases, a return of 200, when you really received 304, is fine - in the end, the server accepted the request without problems and simply reports that there were no changes. However, for any of your needs, you really need to see 304, which is valid, but we must ignore the cache response to do this.

So, when you build your query, you will follow the Alamofire documentation to create something like this:

 let URL = NSURL(string: "https://httpbin.org/post")! let mutableURLRequest = NSMutableURLRequest(URL: URL) mutableURLRequest.HTTPMethod = "POST" let parameters = ["foo": "bar"] do { mutableURLRequest.HTTPBody = try NSJSONSerialization.dataWithJSONObject(parameters, options: NSJSONWritingOptions()) } catch { // No-op } mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type") 

What a regular request looks like. However, we need to override cachePolicy for mutableURLRequest as follows:

 mutableURLRequest.cachePolicy = .ReloadIgnoringLocalAndRemoteCacheData 

After that, just delete it in Alamofire to send:

 Alamofire.request(mutableURLRequest) 
+1
source share

All Articles