如何从iOS访问AWS API网关模型

时间:2016-03-01 16:54:33

标签: amazon-web-services aws-lambda aws-api-gateway vtl

AWS模型架构

{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "QuestionsModel",
    "type": "array",
    "items": {
    "type": "object",
    "properties": {
        "placeholder" : { "type":"string" },
        "type" : { "type":"string" },
        "order": { "type": "integer" },
        "prompt": { "type": "string" },
        "section_name": { "type": "string" }
        }
    }
}

AWS集成响应 - 映射模板 - application / json

使用Velocity模板语言进行映射 数组...

#set($inputRoot = $input.path('$'))
[
#foreach($elem in $inputRoot)
{
  "type" : "$elem.type",
  "placeholder" : "$elem.placeholder",
  "order" : "$elem.order",
  "prompt" : "$elem.prompt",
  "section_name" : "$elem.section_name"
} 
#if($foreach.hasNext),#end
#end
]

AWS Lambda函数

def lambda_handler(event, context):
    client = boto3.client('dynamodb')

    response = client.scan(
        TableName='Question',
        AttributesToGet=[
            'type',
            'order',
            'section_name',
            'prompt',
            'placeholder'])

    return = response['Items']

iOS app the Model

iOS模型的字段type的NSString类型填充了值{S=Hello World}

我宁愿iOS字段等于Hello World,这样我就可以解析{S=*}

我哪里错了?

2 个答案:

答案 0 :(得分:0)

您是否在方法响应中设置了响应模型? 以下是API Gateway提供的基本演练。 http://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-models.html

答案 1 :(得分:0)

I pinned down the answer in another question.

Undocumented but you can simply specify the type after the field name in a mapping template:

#set($inputRoot = $input.path('$'))
[
#foreach($elem in $inputRoot)
{
  "field1" : "$elem.field1.S",
  "field2" : $elem.field2.N
}#if($foreach.hasNext),#end
#end
]

Note that string fields need to be delimited in quotes and numbers don't.