본문 바로가기
카테고리 없음

[AWS] api gateway GET 방식 오류 해결

by 그래놀라_ 2020. 12. 3.

import json

import boto3

 

def lambda_handler(event, context):

    dynamodb = boto3.resource('dynamodb', region_name='us-east-1')

    table = dynamodb.Table('User')

    /*

       api gateway 쓸 때 pathParameters 해줘야 하고 

       처음에 testcase에 'name' : "/jungyr24" 이렇게 슬래시까지 포함돼있어서 오류 났다 슬래시빼고 해결

    */

    resp = table.get_item(Key={"Name": event['pathParameters']['name']})

    print("item type", type(resp['Item']))

    

    return {

            "statusCode":200,

            # "headers": {

            #     "Content-Type":"application/json",

            #     "Access-Control-Allow-Headers" : "Content-Type",

            #     "Access-Control-Allow-Origin": "*",

            #     "Access-Control-Allow-Methods": "OPTIONS,POST,GET",

            #     "Access-Control-Allow-Credentials": "true"

            # },

     /*

        응답 형식이 자꾸 잘못 됐다고 해서 json형식으로 바꿔주니까 해결됨 . dict -> json타입으로 바꿔줘야된다

     */

            "body": json.dumps(resp['Item'])

        }

 

잘 되는 모습.