REST PUT request 'body' ignored, only path parameter is passed when I request via curl

0

Hello,

I configured API gateway & lambda function to update one of my dynamodb table.

Completed testing with API gateway menu, so also tried with curl, but it fail.

Checked cloud watch log, I only can see path parameter, body is not passed correctly.

How can I fix it? As I know, PUT request could have body to update database table attribute value, but it's not in my case.

I also configured 'use lambda proxy integration' option in 'integration request'.

For better understanding, I also add my configuration in below.

Resource

/card/{card_no}

GET

DELETE

PUT <-- this is the problem

tested by API gateway test client

INIT_START Runtime Version: python:3.9.v16 Runtime Version ARN: xxxx

START RequestId: xxxx Version: $LATEST

Event:

{

"resource": "/card/{card_no}",

"path": "/card/1",

"httpMethod": "PUT",

"headers": null,

"multiValueHeaders": null,

"queryStringParameters": null,

"multiValueQueryStringParameters": null,

"pathParameters": {

    "card_no": "1"

},

...

"body": "{\n    \"card_no\": 1,\n    \"nickname\": \"name\",\n    \"overall_type\": \"type\"\n}",

"isBase64Encoded": false

}

END RequestId: xxxx

REPORT RequestId: xxxx Duration: 1322.79 ms Billed Duration: 1323 ms Memory Size: 128 MB Max Memory Used: 66 MB Init Duration: 236.32 ms

tested by curl

curl -v -X PUT \

'https://xxxx.amazonaws.com/dev/card/1' \

-H 'content-type: application/json' \

-d '{"card_no": 1,"nickname": "nickname","overall_type": "type"}'

Trying xxx..

Connected to xxxx (xxxx) port 443 (#0)

ALPN: offers h2

ALPN: offers http/1.1

....

Using HTTP2, server supports multiplexing

Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0

h2h3 [:method: PUT]

h2h3 [:path: /dev/card/1]

h2h3 [:scheme: https]

h2h3 [:authority: xxxx.amazonaws.com]

h2h3 [user-agent: curl/7.86.0]

h2h3 [accept: /]

h2h3 [content-type: application/json]

h2h3 [content-length: 60]

Using Stream ID: 1 (easy handle 0x14180c600)

PUT /dev/card/1 HTTP/2

Host: xxxx.amazonaws.com

user-agent: curl/7.86.0

accept: /

content-type: application/json

content-length: 60

Connection state changed (MAX_CONCURRENT_STREAMS == 128)!

We are completely uploaded and fine

HTTP/2 200

date: xxx

content-type: application/json

content-length: 220

x-amzn-requestid: xxxx

x-amz-apigw-id: xxxx

x-amzn-trace-id: Root=xxxx

Connection #0 to host 3pjqiu4m22.execute-api.ap-northeast-2.amazonaws.com left intact

{"errorMessage": "'body'", "errorType": "KeyError", "requestId": "xxxx", "stackTrace": [" File "/var/task/index.py", line 12, in handler\n body_input = json.loads(event['body'])\n"]}%

cloud watch log when I send curl

INIT_START Runtime Version: python:3.9.v16 Runtime Version ARN: xxxx

START RequestId: xxxx Version: $LATEST

Event:

{

"card_no": 1

} ==> strange point, I added print in my python code to see all the request, but only path parameter passed, can't see body...

[ERROR] KeyError: 'body'

Traceback (most recent call last):

File "/var/task/index.py", line 12, in handler

body_input = json.loads(event['body'])

END RequestId: xxxx

REPORT RequestId: xxxx Duration: 1024.82 ms Billed Duration: 1025 ms Memory Size: 128 MB Max Memory Used: 64 MB Init Duration: 226.62 ms

lambda code

import json
import boto3

def handler(event, context) :
  print("Event: %s" % json.dumps(event))
  client = boto3.resource('dynamodb')
  table = client.Table('CardInfo')

  body_input = json.loads(event['body'])
  
  response = table.update_item(
      xxx...xxx
    },
    ReturnValues="UPDATED_NEW"
  )

  return {
    'statusCode': response['ResponseMetadata']['HTTPStatusCode'],
    'body': json.dumps(response['Attributes'], default=str)
  }
1 Answer
0

On the stage, can you enable Full Logs to see the payload that is being submitted into the API? If it's working in the console but not in cURL something seems off on the cURL command but I don't see anything at first pass.

Post what the full logs generate in CloudWatch when calling from cURL.

profile picture
bpyle
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions