2016-03-28 10 views
3

Ich habe eine Lambda-Funktion und API Gateway-Endpunkt erstellt, so dass es die Abfrage und Header-Parameter, die es empfängt, und ich möchte die gesamte Nutzlast zu JSON für Verwaltbarkeit zu analysieren.Parse AWS API Gateway-Header in Lambda

Die empfangenen Nutzdaten in dieser Form ist:

"{Accept=*/*, 
Accept-Encoding=gzip, 
deflate, 
Accept-Language=nb-NO,nb;q=0.8,no;q=0.6,nn;q=0.4,en-US;q=0.2,en;q=0.2,sv;q=0.2,da;q=0.2, 
Authorization=COzTjCKD6VHTC, 
Cache-Control=no-cache, 
User-Agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36, 
Via=1.1 7822a0bcea47c939c09def064206add3.cloudfront.net (CloudFront), X-Amz-Cf-Id=Bd_gFYsmhx0jK0eKf-3sZwwRozXtFoYC5UEFDDLKWYJkq6AR_L0Cfw==, 
X-Forwarded-For=89.8.222.70, 205.251.218.72, 
X-Forwarded-Port=443, X-Forwarded-Proto=https}" 

Es ist nicht trivial dies manuell zu analysieren (es gibt in den Saiten kein Entkommen). Welches Format ist das und gibt es einige Node-Bibliotheken, die dieses Format für JSON parsen?

Mein requestTemplate:

"requestTemplates": { 
    "application/json": "{\"httpMethod\": \"$context.httpMethod\", \"route\": \"$input.params('route')\", \"query\": \"$input.params().querystring\", \"header\": \"$input.params().header\"}" 
    }, 

Antwort

2

Sie finden es einfacher, die [Methode anfordern Pass-Through] Vorlage zu verwenden (erhältlich über die Vorlage Dropdown in der Konsole generieren), die die Werte in ein Wörterbuch verwandeln:

## See http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html 
#set($allParams = $input.params()) 
{ 
"body-json" : "$input.json('$')", 
"params" : { 
#foreach($type in $allParams.keySet()) 
    #set($params = $allParams.get($type)) 
"$type" : { 
    #foreach($paramName in $params.keySet()) 
    "$paramName" : "$util.escapeJavaScript($params.get($paramName))" 
     #if($foreach.hasNext),#end 
    #end 
} 
    #if($foreach.hasNext),#end 
#end 
}, 
"stage-variables" : { 
#foreach($key in $stageVariables.keySet()) 
"$key" : "$util.escapeJavaScript($stageVariables.get($key))" 
    #if($foreach.hasNext),#end 
#end 
}, 
"context" : { 
    "account-id" : "$context.identity.accountId", 
    "api-id" : "$context.apiId", 
    "api-key" : "$context.identity.apiKey", 
    "authorizer-principal-id" : "$context.authorizer.principalId", 
    "caller" : "$context.identity.caller", 
    "cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider", 
    "cognito-authentication-type" : "$context.identity.cognitoAuthenticationType", 
    "cognito-identity-id" : "$context.identity.cognitoIdentityId", 
    "cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId", 
    "http-method" : "$context.httpMethod", 
    "stage" : "$context.stage", 
    "source-ip" : "$context.identity.sourceIp", 
    "user" : "$context.identity.user", 
    "user-agent" : "$context.identity.userAgent", 
    "user-arn" : "$context.identity.userArn", 
    "request-id" : "$context.requestId", 
    "resource-id" : "$context.resourceId", 
    "resource-path" : "$context.resourcePath" 
    } 
} 
+0

Ist das ein neues Feature in der Konsole? Ich mag es, wie es all diese Informationen im "Kontext" weitergibt. –

+1

Vielen Dank, sieht gut aus! Aber wo ist das Drop-Down? –

+0

@MarkB Dies wurde kürzlich in der Konsole hinzugefügt. Beim Ändern einer Vorlage gibt es zwei Dropdown-Menüs. Diese neue Vorlage ist verfügbar unter ** ** Vorlage generieren ** als ** [Methode Request Passthrough] **. –

2

Wenn Sie diese Mapping-Vorlage in API-Gateway wird es für Sie analysiert werden, und als event.headers.Accept-Encoding in der Lambda-Funktion zur Verfügung, event.headers.Accept-Language usw.

{ 
    "method": "$context.httpMethod", 
    "body" : $input.json('$'), 
    "headers": { 
    #foreach($param in $input.params().header.keySet()) 
    "$param": "$util.escapeJavaScript($input.params().header.get($param))" #if($foreach.hasNext),#end 

    #end 
    }, 
    "queryParams": { 
    #foreach($param in $input.params().querystring.keySet()) 
    "$param": "$util.escapeJavaScript($input.params().querystring.get($param))" #if($foreach.hasNext),#end 

    #end 
    }, 
    "pathParams": { 
    #foreach($param in $input.params().path.keySet()) 
    "$param": "$util.escapeJavaScript($input.params().path.get($param))" #if($foreach.hasNext),#end 

    #end 
    }, 
    "stage" : "$context.stage" 
} 

Hinweis dass ich das von kennbrodhagens ausgezeichneter Antwort auf diese Frage bekommen habe: How to pass a querystring or route parameter to AWS Lambda from Amazon API Gateway und ich habe gerade die Eigenschaft "stage" hinzugefügt, um die API Gateway Stage in der Lambda-Funktion verfügbar zu machen.