2016-07-15 129 views
2

我想從處理程序返回值到API網關響應標頭。Lambda和API網關映射

Handler.js

module.exports.handler = function(event, context, cb) { 
    const UpdateDate = new Date(); 
    return cb(null, { 
    body: { 
     message: 'test' 
    }, 
    header: { 
     Last-Modified: UpdateDate 
    } 
    }); 
}; 

S-function.json在 「端點」

"responses": { 
    "400": { 
     "statusCode": "400" 
    }, 
    "default": { 
     "statusCode": "200", 
     "responseParameters": { 
     "method.response.header.Cache-Control": "'public, max-age=86400'", 
     "method.response.header.Last-Modified": "integration.response.body.header.Last-Modified" 
     }, 
     "responseModels": { 
     "application/json;charset=UTF-8": "Empty" 
     }, 
     "responseTemplates": { 
     "application/json;charset=UTF-8": "$input.json('$.body')" 
     } 
    } 
    } 

這可以工作。但我想知道如何使用「integration.response.header.Last-Modified」。我的處理程序回調formate是否錯誤?

編輯: S-function.json在 「端點」

「integration.response.header.Last修飾」 這是行不通的。 我想知道具體的處理返回合成將數據傳遞給「integration.response.header.Last-Modified」。

"responses": { 
    "400": { 
     "statusCode": "400" 
    }, 
    "default": { 
     "statusCode": "200", 
     "responseParameters": { 
     "method.response.header.Cache-Control": "'public, max-age=86400'", 
     "method.response.header.Last-Modified": "integration.response.header.Last-Modified" 
     }, 
     "responseModels": { 
     "application/json;charset=UTF-8": "Empty" 
     }, 
     "responseTemplates": { 
     "application/json;charset=UTF-8": "$input.json('$.body')" 
     } 
    } 
    } 
+0

這看起來乍一看正確的,但我不明白你的問題。你是說這個*不工作嗎?如果不是,你看到了什麼行爲? –

+1

我想使用「integration.response.header.Last-Modified」,我不知道lambda歸還合成。 – Jim

回答

0

您的lambda函數的所有輸出都返回到響應正文中,因此您需要將響應正文的一部分映射到您的API響應標頭。

module.exports.handler = function(event, context, cb) { 
    const UpdateDate = new Date(); 
    return cb(null, { 
     message: 'test', 
     Last-Modified: UpdateDate 
    }); 
}; 

會產生有效載荷 「{」 消息 「: 」測試「, 」上次修改「:」 ... 「}」

在這種情況下,你就可以用「integration.response.body。 Last-Modified「作爲映射表達式。作爲一個附註,在響應主體中命名「body」和「header」可能會使映射表達式混淆讀取。

感謝, 瑞安

+0

謝謝!這對我很有幫助。 – Jim