简体   繁体   English

在AWS ApiGateway上找不到JSONObject [\\“ body \\”]

[英]JSONObject[\“body\”] not found at AWS ApiGateway

I'm developing an API Gateway to get data (image) from a sensor and send to DynamoDB at AWS. 我正在开发API网关,以从传感器获取数据(图像)并发送到AWS的DynamoDB。 I already have the endpoint and API key. 我已经有了端点和API密钥。 However, I get the following error when testing the API: 但是,在测试API时出现以下错误:

Request: /?image=12345  
Status:  
Latency: ms  
Response Body  
{  
  "cause": "JSONObject[\"body\"] not found.",  
  "logref": "12345-11d8-888888",  
  "message": ""  
}  

IAM roles and policies are ok. IAM角色和策略都可以。 The Python code posts via requests the following payload Python代码通过requests以下有效载荷发布

payload={"image": [1,2,3,4,5]}

Given that: 鉴于:

requests.posts(API_URL,files=payload,headers={'api-key':'12345abcd'})

My Body Mapping Template is: 我的身体映射模板是:

{"body" : $input.json('$')}

But this Body Mapping is generating an alert in Request Body of GET (inappropriate request validator). 但是,此主体映射在GET的请求主体(不适当的请求验证器)中生成警报。

As you can see in the following image, I'm getting 4xx errors for all API Requests. 如下图所示,所有API请求都收到4xx错误。

在此处输入图片说明

DynamoDB is not receiving the payload. DynamoDB没有收到有效负载。 Can someone give me a slight idea of the possible causes for this? 有人可以给我一些可能的原因吗?

I found the solution. 我找到了解决方案。 There were a couple of things to be fixed: 有几件事需要解决:

1 - I mapped the JSON correctly using: https://www.liquid-technologies.com/online-json-to-schema-converter 1-我使用以下方法正确映射了JSON: https//www.liquid-technologies.com/online-json-to-schema-converter

So, for: 因此对于:

{
  "MessageID" : {
    "S" : "12345"
  },
  "type" : {
"S" : "type"
  },
  "page" : {
"S" : "page"
  }
}

... sent by Python, the body mapping became: ...由Python发送,正文映射变为:

{
  "MessageID" : {
    "S" : "$input.params('MessageID')"
  },
  "type" : {
    "S" : "$input.params('type')"
  },
  "page" : {
    "S" : "$input.params('page')"
  }
}

And the model: 和模型:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "MessageID": {
      "type": "object",
      "properties": {
        "S": {
          "type": "string"
        }
      },
      "required": [
        "S"
      ]
    },
    "type": {
      "type": "object",
      "properties": {
        "S": {
          "type": "string"
        }
      },
      "required": [
        "S"
      ]
    },
    "page": {
      "type": "object",
      "properties": {
        "S": {
          "type": "string"
        }
      },
      "required": [
        "S"
      ]
    }
  },
  "required": [
    "MessageID",
    "type",
    "page"
  ]
}

Now I get: 现在我得到:

<Response [200]>
ok
<Response [200]>
ok
<Response [200]>
ok
<Response [200]>
ok
<Response [200]>
ok
<Response [200]>
ok
<Response [200]>
ok
<Response [200]>
ok

And data is arriving at DynamoDB via Lambda function, clean and formatted, with no logs errors in API Gateway. 数据通过Lambda函数经过清理和格式化后到达DynamoDB,API Gateway中没有日志错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM