0
votes

I defined my REST API using AWS API Gateway and generated client code for iOS. When I call a method the SDK outputs this error message:

AWSiOSSDKv2 [Error] 
AWSAPIGatewayClient.m line:190
__118-[AWSAPIGatewayClient invokeHTTPRequest:URLString:pathParameters:
queryParameters:headerParameters:body:responseClass:]_block_invoke_2 | 
Failed to serialize the body JSON. 
(null)

What is wrong?

1

1 Answers

1
votes

Easy!

  1. Make sure your AWSModel has the same number of class members as the number of JSON key paths property keys. Mine has no class member and 2 property keys.

  2. Make sure the name of each property key matches the name of the class member. Again I had a key for "code" and no matching "code" property.

For clarity, look at the JSONKeyPathsByPropertyKey function. If you see @"abc": @"def" then you must have a property "abc" in your class otherwise the JSON conversion will fail.

// Sample JSON returned by AWS API Gateway
{"code":200, "message":"OK", "data":{"phone":"(555) 555-1234"}}
// APISample.h

#import 
#import 

@interface APISample : AWSModel

// We count 4 class members
@property (nonatomic, strong) NSNumber *code;
@property (nonatomic, strong) NSString *message;
@property (nonatomic, strong) NSDictionary *data;
@property (nonatomic, strong) NSNumber *phone;

@end
// APISample.m

#import "APISample.h"

@implementation APISample

// We count 4 property keys
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
             @"code": @"code",
             @"message": @"message",
             @"data": @"data",
             @"phone": @"data.phone"
             };
}

Tip: Notice how you can access a branch (data as NSDictionary) and traverse the document structure with dot notation (data.phone).

Bonus: a working Swift example just for you.

// Swift sample code to access AWS API Gateway under iOS

// Create a client with public access
var client : APISampleClient = APISampleClient.defaultClient()

// Comment next line if your API method does not need API key
client.APIKey = "Your API key"

client.SampleMethodGet().continueWithBlock { (task : AWSTask) -> AnyObject? in

  if task.error != nil { 
    print("Error \(task.error)") 
  }
  else if task.result != nil {
    let output = task.result as! APISample
    print("Success \(output)")
  }
  return nil
}