ios - Alamofire response not matching request -
i having issue when making post request api via alamofire, gets work without issue, whenever make post when check response results of last get.
import alamofire import swiftyjson class networkmanager { static let sharedinstace = networkmanager() let defaultmanager: alamofire.manager = { let servertrustpolicies: [string: servertrustpolicy] = [ "homestead.app": .disableevaluation ] let configuration = nsurlsessionconfiguration.defaultsessionconfiguration() configuration.httpadditionalheaders = alamofire.manager.defaulthttpheaders return alamofire.manager( configuration: configuration, servertrustpolicymanager: servertrustpolicymanager(policies: servertrustpolicies) ) }() } internal class apihelper { /** data target url , return json data parsed - parameter targeturl: url pull data - parameter success: return data calling function - parameter failure: return error message calling function */ private func getdatafromapi(targeturl: string, success:(jsondata: json) -> (), failure:(message: string) -> ()) { networkmanager.sharedinstace.defaultmanager.request(.get, targeturl).responsejson { response in print(response.result) switch response.result { case .success: if let jsonraw = response.result.value { let json = json(jsonraw) success(jsondata: json) } case .failure(let error): print(error.localizeddescription) failure(message: error.localizeddescription) } } } /** post data target url , return errors json data parsed - parameter targeturl: url post - parameter parameters: json data post - parameter success: return success message calling function - parameter failure: return json data calling function server error */ private func postdatatoapi(targeturl: string, parameters: [string : anyobject], success:() -> (), failure:(jsondata: json) -> ()) { networkmanager.sharedinstace.defaultmanager.request(.post, targeturl, parameters: parameters, encoding: .json).responsejson { response in debugprint(response) success() } } /** post updated profile api - parameter parameters: json data posted - parameter success: success callback - parameter failure: json data of servererror */ internal func postupdaterequest(parameters: [string : anyobject], success:() -> (), failure:(jsondata: json) -> ()) { let url = "https://homestead.app/profile/a/update" postdatatoapi(url, parameters: parameters, success: { success() }, failure: { jsondata in failure(jsondata: jsondata) }) } /** states api - parameter success: json data of states - parameter failure: failure message */ internal func getallstates(success:(jsondata: json) -> (), failure:(message: string) -> ()) { let url = "https://homestead.app/api/state/all" getdatafromapi(url, success: { jsondata in success(jsondata: jsondata) }, failure: { message in failure(message: message) }) } } let api = apihelper() api.getallstates({ jsondata in print(jsondata) let params: [string : anyobject] = ["name" : "bob"] api.postupdaterequest(params, success: { jsondata in print("success") }, failure: { message in print("message") }) }, failure: { message in print(message) })
my code first gets list of states , posts updated user profile. issue in when response updated user profile, includes response earlier request had been completed. post goes through , changes made in web services, have no indications in response object.
i have confirmed server not return list of states when making post request, returns below when called manually browser:
{ "success": "changes saved!" }
i'm @ loss on why getting response earlier request post. thoughts?
i figured out. turned out had add "x-requested-with": "xmlhttprequest" requests header:
configuration.httpadditionalheaders = [ "x-requested-with": "xmlhttprequest" ]
now getting responses correctly server.
Comments
Post a Comment