Parse JSON when I get a error response in Alamofire - json

I've been working on API requests using Alamofire, and I want to know something for parsing the Error and to get the JSON data from the server instead of accessing the AFErrors. The code below works fine, but since I've been using .responseDecodable for decoding the response and also the MyError inherits Decodable, I was wondering if there is a similar way to decode data when I get the error response back, instead of using JSONDecoder().decode.
When I get the error response, it returns a JSON object with one value in it (message) from the server.
static let sessionManager: Session = {
let configuration = URLSessionConfiguration.af.default
configuration.timeoutIntervalForRequest = 30
return Session(configuration: configuration)
}()
Request.sessionManager.request(endpoint, method: httpMethod, parameters: params, headers: headers)
.responseDecodable(of: resDecodeType.self) { response in
switch response.result {
case .success(let successResponse):
completed(.success(successResponse))
case .failure(let error):
let errorStatusCode = response.response?.statusCode
do {
let data = try JSONDecoder().decode(MyError.self, from: response.data!) // -> this part...
} catch {
print(error)
}
}
}
}
struct MyError: Decodable {
let message: String
}

Related

Issue To Create A Base Service <Alamofire,PromiseKit,Generic>

Hello everyone I want to crate a base api service for my apps but when I try a lot of different way but i can't solve my problems. I have to use alamofire, promiseKit and generic's.
My first solution and his issue: I can get my json from api but when I try to decode my json every time it fails. There is the code:
func fetchData<T: Codable>(_ page: Int) -> Promise<T> {
let path = getPath(page)
return Promise<T> { seal in
AF.request(path, method: .get).validate(statusCode: 200..<300).responseString { response in
switch response.result {
case .success(let data):
if let json = data.data(using: .utf8) {
do {
let res = try JSONDecoder().decode(T.self, from: json)
print(res)
seal.fulfill(res)
} catch {
print("json serilaztion error")
print(error)
seal.reject(error)
}
}
case .failure(let error):
seal.reject(error)
}
}
}
}
My second solution and issue : When I do my request I get my json response and decode it but in this solution I can't return data with seal.fulfill(data) gives me "Cannot convert value of type 'Any' to expected argument type 'T'" and when I try to seal.fulfill(data as! T) like this when I run app always crashes. There is my code :
func fetchData<T: Codable>(_ page: Int) -> Promise<T> {
let path = getPath(page)
return Promise<T> { seal in
AF.request(path, method: .get).validate(statusCode: 200..<300).responseJSON { response in
switch response.result {
case .success(let data):
seal.fulfill(data as! T) // Cannot convert value of type 'Any' to expected argument type 'T'
case .failure(let error):
seal.reject(error)
}
}
}
}
Finally I'm using test api https://www.flickr.com/services/api/explore/flickr.photos.getRecent to test my code. How I can solved this issue's ? Thanks for your helps
Use responseDecodable instead of responseString or responseJSON.
AF.request(...).responseDecodable(of: T.self) { response in
...
}

Stringified JSON to Swift Object or JSON dictionary

In my Swift code I make a URLRequest to my node.js server:
URLSession.shared.dataTask(with: checkoutRequest, completionHandler: {
[weak self] (data: Data?, response: URLResponse?, error: Error?) in
guard let data = data,
let dataString = String(data: data, encoding: String.Encoding.utf8) else {
return
}
// Help me!!!
}).resume()
The node.js handles this request by processing a transaction through the Braintree Payments checkout API.
checkoutProcessor.processCheckout(amount, nonce, (error, result) => {
// Checkout callback
if (error) {
res.write(error.message)
res.end()
} else if (result) {
console.log(result)
res.write(JSON.stringify(result))
res.end()
}
})
As usual, if the API request fails (e.g., no signal) it returns an error but if the transaction goes through, it returns a result.
The type of the result, however, depends on whether the financial transaction fails or succeeds:
For example, the result for a successful transaction:
Object {transaction: Transaction, success: true}
result for failed transaction:
ErrorResponse {errors: ValidationErrorsCollection, params: Object, message: "Insufficient Funds", transaction: Transaction, success: false}
The dataString winds up looking like this:
{\"transaction\":{\"id\":\"m7mj3qd7\",\"status\":\"submitted_for_settlement\",\"type\":\"sale\",\"currencyIsoCode\":\"USD\",\"amount\":\"12.34\",\"merchantAccountId\":\"yourpianobar\",\"subMerchantAccountId\":null,\"masterMerchantAccountId\":null,\"orderId\":null,\"createdAt\":\"2018-09-19T03:30:27Z\",\"updatedAt\":\"2018-09-19T03:30:27Z\",\"customer\":{\"id\":\"622865439\",\"firstName\":\"Test\",\"lastName\":\"FromSwiftTest\"
which certainly resembles a JSON object but I can't seem to decode it with JSONDecoder, doing so fails. (JSONEncoder also fails)
Most solutions I see for Objectifying stringified JSON data into Swift involves writing a swift struct into which to plop all the JSON object's properties, but since this the data structure of the result is unknown on the swift end, I don't know what to do.
How do I get these objects into my swift code?
Note: I've also tried just sending res.send(result) in the node.js code but that doesn't really change anything.
This would do the trick for Swift 5:
if let data = dataString.data(using: String.Encoding.utf8) {
do {
if let dictionary = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Any] {
// Use this dictionary
print(dictionary)
}
} catch _ {
// Do nothing
}
}
You can use JSONSerialization class on your data to convert it from data to Dictionary/Array based on your json response. The code can look something like below (based on my understanding) in swift 4
URLSession.shared.dataTask(with: checkoutRequest) { (data, response, error) in
guard let requestData = data, error == nil, let httpResponse = response as? HTTPURLResponse else {
// handle error
return
}
do {
if httpResponse.statusCode == 200 {
// success json
let jsonObject = try JSONSerialization.jsonObject(with: requestData, options: .allowFragments)
print(jsonObject) // jsonObject can be dictionary or Array. Typecast it based on your response
} else {
//error json
let jsonObject = try JSONSerialization.jsonObject(with: requestData, options: .allowFragments)
print(jsonObject) // jsonObject can be dictionary or Array. Typecast it based on your response
}
}
catch {
print(error.localizedDescription)
}
}.resume()

How do I access a nested JSON value using Alamofire and SwiftyJSON?

I am trying to access nested JSON results using swiftyJSON and Alamofire. My print value is nill and I believe I am not doing this correctly. What should my parameters be? I am trying to get the quote value located at http://quotes.rest/qod.json
func getAPI() {
Alamofire.request(.GET, "http://quotes.rest/qod.json", parameters: ["contents": "quotes"])
.responseJSON { response in
if let JSON = response.result.value {
print(JSON["quote"])
}
}
}
In your JSON quotes is an array so if you want to access quote of the first object you should do it by accessing first object:
func getAPI() {
Alamofire.request(.GET, "http://quotes.rest/qod.json", parameters: ["contents": "quotes"])
.responseJSON { response in
if let jsonValue = response.result.value {
let json = JSON(jsonValue)
if let quote = json["contents"]["quotes"][0]["quote"].string{
print(quote)
}
}
}
}
If the syntax of the json isn't correct, since it is fully printed anyway you should notice what's wrong.
func getAPI() {
Alamofire.request(.GET, "http://quotes.rest/qod.json", parameters: ["contents": "quotes"])
// JSON response
.responseJSON { response in switch response.result {
case .Failure(let error):
// got an error in getting the data, need to handle it
print("error calling GET, json response type :")
// print alamofire error code
let statusCode = error.code
print("error code json : \(statusCode)")
// print json response from server
if let data = response.data {
print("Response data: \(NSString(data: data, encoding: NSUTF8StringEncoding)!)")
}
// print http status code plus error string
print(NSHTTPURLResponse.localizedStringForStatusCode(statusCode))
if let httpResponse : NSHTTPURLResponse = response.response {
print("HTTP Response statusCode: \(httpResponse.statusCode)")
}
case .Success( _):
let statusCode = (response.response?.statusCode)!
print("status code json : \(statusCode)")
print("there is a response json")
//print(value)
// parse the result as JSON, since that's what the API provides and save datas as new user in coreData
guard let data = response.data else {
print("Error parsing response data")
return
}
let json = JSON(data: data)
// access first element of the array
if let postContent = json["contents"]["quotes"][0]["quote"].string{
// deal with json
}
}
}

How to convert <AnyObject> response in AnyObject after an Alamofire request with JSON in Swift?

So I want to send a request to a specific API which is supposed to return a JSON file.
I am using Alamofire in order to get the JSON object :
dataFromAPI : JSON
Alamofire.request(.GET, myURL).responseJSON { (_, _, data) -> Void in
dataFromAPI = JSON(data)
}
My problem is that data is an array of AnyObject and the JSON function needs an AnyObject type. How can I transform one into another or resolve the problem ?
Not sure if I got your question, but will try to provide you an example of how I do it.
Changed code to your case.
Alamofire.request(.GET, myURL)
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseJSON { request, response, jsonResult in
switch jsonResult {
case .Success:
if let data = jsonResult.value as? AnyObject {
self.dataFromAPI = JSON(data)
}
case .Failure(_, let error):
print(error)
}
}
Normally I wouldn't do unwrapping to AnyObject, as it makes little sense.
I usually unwrap to [String: AnyObject] as I'm expecting Dictionary from my API, and then I attempt to convert it to my custom model class.
Correct me if I miss-understood the question. :)
Alamofire returns a Result<AnyObject> object. You should check if the result is a success or a failure before extracting the JSON:
Alamofire.request(.GET, myURL)
.responseJSON { request, response, result in
switch result {
case .Success(let JSON):
print("Success with JSON: \(JSON)")
case .Failure(let data, let error):
print("Request failed with error: \(error)")
if let data = data {
print("Response data: \(NSString(data: data, encoding: NSUTF8StringEncoding)!)")
}
}
}

Alamofire POST request with Swift 2

I am trying to make a POST request in Alamofire to return a JSON object. This code worked in Swift 1, but in swift 2 I get this invalid parameter issue:
Tuple types '(NSURLRequest?, NSHTTPURLResponse?, Result<AnyObject>)' (aka '(Optional<NSURLRequest>, Optional<NSHTTPURLResponse>, Result<AnyObject>)') and '(_, _, _, _)' have a different number of elements (3 vs. 4)
It seems like the error parameter was removed, but I am using the error parameter inside the function to check for errors, so how would I do that without an error param?
Here's my code for the POST request:
let response = Alamofire.request(.POST, urlPath, parameters: parameters, encoding: .URL)
.responseJSON { (request, response, data, error) in
if let anError = error
{
// got an error in getting the data, need to handle it
print("error calling POST on /posts")
print(error)
}
else if let data: AnyObject = data
{
// handle the results as JSON, without a bunch of nested if loops
let post = JSON(data)
// to make sure it posted, print the results
print("The post is: " + post.description)
}
}
If you see the documentation in the branch Swift2.0 you can see that the responseJSON function has changed as the error says, it have now three parameters but you can catch the error too, lets take a look:
public func responseJSON(
options options: NSJSONReadingOptions = .AllowFragments,
completionHandler: (NSURLRequest?, NSHTTPURLResponse?, Result<AnyObject>) -> Void)
-> Self
{
return response(
responseSerializer: Request.JSONResponseSerializer(options: options),
completionHandler: completionHandler
)
}
Now it returns an enum Result<AnyObject> and according to the doc :
Used to represent whether a request was successful or encountered an error.
- Success: The request and all post processing operations were successful resulting in the serialization of the
provided associated value.
- Failure: The request encountered an error resulting in a failure. The associated values are the original data
provided by the server as well as the error that caused the failure.
And it have inside an property entitled error, with the following doc:
/// Returns the associated error value if the result is a failure, `nil` otherwise.
public var error: ErrorType? {
switch self {
case .Success:
return nil
case .Failure(_, let error):
return error
}
}
Then if you follow this test case inside Alamofire you can see how to get the error properly:
func testThatResponseJSONReturnsSuccessResultWithValidJSON() {
// Given
let URLString = "https://httpbin.org/get"
let expectation = expectationWithDescription("request should succeed")
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var result: Result<AnyObject>!
// When
Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
.responseJSON { responseRequest, responseResponse, responseResult in
request = responseRequest
response = responseResponse
result = responseResult
expectation.fulfill()
}
waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
// Then
XCTAssertNotNil(request, "request should not be nil")
XCTAssertNotNil(response, "response should not be nil")
XCTAssertTrue(result.isSuccess, "result should be success")
XCTAssertNotNil(result.value, "result value should not be nil")
XCTAssertNil(result.data, "result data should be nil")
XCTAssertTrue(result.error == nil, "result error should be nil")
}
UPDATE:
Alamofire 3.0.0 introduces a Response struct. All response serializers (with the exception of response) return a generic Response struct.
public struct Response<Value, Error: ErrorType> {
/// The URL request sent to the server.
public let request: NSURLRequest?
/// The server's response to the URL request.
public let response: NSHTTPURLResponse?
/// The data returned by the server.
public let data: NSData?
/// The result of response serialization.
public let result: Result<Value, Error>
}
So you can call it like the following way:
Alamofire.request(.GET, "http://httpbin.org/get")
.responseJSON { response in
debugPrint(response)
}
You can read more about the migration process in the Alamofire 3.0 Migration Guide.
I hope this help you.
Have you tried using three parameters in the .responseJSON and inserting a try catch block around the areas you want error logging, check out this link if you need help with swift 2 try catchs