2016-06-08 4 views
7

어떤 이유로 든 event에서 원시 본문을 가져 오는 데 어려움을 겪고 있습니다. application/json 콘텐츠 유형에 대해 $input.body을 json으로 로깅합니다. 여기 The docs say that that should contain the raw payload.AWS API 게이트웨이 본문 매핑 템플릿의 기본 페이로드

내 통합 요청 바디 매핑 템플릿 :이 요청에서 원시 몸에 액세스 할 수있는 방법이

{ 
    "event": { 
    "body": { 
     "hello": "meow" 
    }, 
    "rawBody": { 
     "hello": "meow" 
    }, 
    "headers": { 
     "Accept": "*/*", 
     "Accept-Encoding": "gzip, deflate", 
     "Accept-Language": "en-US", 
     "Cache-Control": "no-cache", 
     "CloudFront-Forwarded-Proto": "https", 
     "CloudFront-Is-Desktop-Viewer": "true", 
     "CloudFront-Is-Mobile-Viewer": "false", 
     "CloudFront-Is-SmartTV-Viewer": "false", 
     "CloudFront-Is-Tablet-Viewer": "false", 
     "CloudFront-Viewer-Country": "US", 
     "Content-Type": "application/json", 
     "Host": "7nuy7lymef.execute-api.us-east-1.amazonaws.com", 
     "Origin": "file://", 
     "Postman-Token": "0ce7c6f4-3864-c9b4-f2db-739737b2ba49", 
     "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Postman/4.2.2 Chrome/47.0.2526.73 Electron/0.36.2 Safari/537.36", 
     "Via": "1.1 1eea0bca59557555878da4d9775c509f.cloudfront.net (CloudFront)", 
     "X-Amz-Cf-Id": "SDjaGcuJ5eVkOMMCn6M3vGaVicA1fuA7h0bUYE4ARlKupO60eeYNFA==", 
     "X-Forwarded-For": "206.71.230.14, 205.251.250.135", 
     "X-Forwarded-Port": "443", 
     "X-Forwarded-Proto": "https", 
     "x_example_header": "my awesome header" 
    }, 
    "method": "POST", 
    "params": {}, 
    "query": { 
     "example_param": "myawesomeparam" 
    } 
    }, 
    "context": { 
    "callbackWaitsForEmptyEventLoop": false, 
    "logGroupName": "/aws/lambda/reggi-log-post", 
    "logStreamName": "2016/06/08/[$LATEST]aad04e0e46614c288ac8ca43d0a95076", 
    "functionName": "reggi-log-post", 
    "memoryLimitInMB": "128", 
    "functionVersion": "$LATEST", 
    "invokeid": "6e4e1e13-2dc1-11e6-a1f7-4dad3a8eb122", 
    "awsRequestId": "6e4e1e13-2dc1-11e6-a1f7-4dad3a8eb122", 
    "invokedFunctionArn": "arn:aws:lambda:us-east-1:562508364089:function:reggi-log-post" 
    } 
} 
  • 있습니까 : 여기

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

    는 페이로드 예제?

  • 모든 유형을 허용하도록 콘텐츠 유형을 변경할 수있는 방법이 있습니까?

답변

0

$input.body에는 원시 페이로드가 들어 있습니다. "rawBody": "$input.body"처럼 따옴표를 써야합니다. 그렇지 않으면 본문은 json 문서의 일부로 해석됩니다.

+0

은 댓글이어야합니다. –

2

다음 블로그 게시물은이 문제를 해결하는 방법에 대해 자세히 설명합니다. https://nicholasjackson.io/2016/12/13/using-graphql-with-aws-lambda/

이것은 GraphQL의 컨텍스트에서 특별히 작성되었지만 모든 콘텐츠 유형에서 작동합니다. 짧게 :

  • 이진 지원 섹션으로 이동하십시오. 선택한 미디어 유형에 대한 바이너리 지원을 활성화하고 저장하십시오.
  • 리소스 절의 방법으로 돌아가서 통합 요청을 엽니 다. 추가/선택한 내용 유형에 몸 매핑 템플릿을 편집하고 다음을 넣어 :

    "rawBody": "$util.escapeJavaScript($util.base64Decode($input.body))"

  • 저장하고 API를 다시 배포합니다.

바이너리 지원을 추가하면 요청이 base64 문자열로 인코딩됩니다. 본문 매핑 템플릿이이를 디코딩합니다.

관련 문제