Therefore, the API I work with sometimes sends an error message to the response body when the request fails. It is located at response.data. Sometimes it is JSON, sometimes it is a string. I use the method validate, so result.valuethere is nilwhen an error occurs.
response.data
validate
result.value
nil
Is there a way for Alamofire to serialize data from NSData to a string or for JSON before [ String : AnyObject ], as if this answer were successful?
[ String : AnyObject ]
I would like to use the validate method.
EDIT: Here is the link to the feature request that I started in the Alamofire GitHub project.
https://github.com/Alamofire/Alamofire/issues/1459
. Alamofire 4. Alamofire 3 response.data , . Alamofire 4 , , response.data , Error, .
Error
, , , ( ). , , response.data, . , . , OAuth2 , .
OAuth2
Swift 4
, json.
import Alamofire import SwiftyJSON Alamofire.request("http://domain/endpoint", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil) .validate() .responseJSON(completionHandler: { response in if let error = response.error { if let data = response.data { if let errorString = String(bytes: data, encoding: .utf8) { print("Error string from server: \(errorString)") } else { print("Error json from server: \(JSON(data))") } } else { print("Error message from Alamofire: \(error.localizedDescription)") } } guard let data = response.result.value else { print("Unable to parse response data") return } print("JSON from server: \(JSON(data))") })
Source: https://habr.com/ru/post/1653628/More articles:Stateless sessions with flask and AWS ELB - pythonHow to prevent multiple form feeds in Yii2? - javascriptExcel VBA Hex Formatting - vbaReact Native application continues to close on Android emulator - androidHow to stop interval from observable - rx-javaWhat is the difference between saveArg and saveArgPointee in gmock? - c ++Html5 Canvas "Composite layers" causing long frames - javascriptWhen to put a space between a tag name and a class or identifier - cssHow to build a dictionary of string lengths using a for loop? - pythonSyntaxError: multiple statements found when compiling a single statement - pythonAll Articles