我无法 post 我的 json 到我的 api lambda function.I 我得到 {"message":"Missing Authentication Token"}

I am unable to post my json to my api lambda function.I am getting {"message":"Missing Authentication Token"}

实际上,当我将 json 导入我的 POSTMAN 应用程序并发送请求时,一切都运行良好。但是当我尝试 POST 使用ajax 呼叫或直接点击 api

我曾尝试删除 api 密钥并进行了所有授权 none

这是我的 API - https://ym4j4pt5mf.execute-api.us-east-1.amazonaws.com/Beta 我正在尝试 post 将其作为原始主体:-

{
  "DestinationBot": "iSearchBot",
  "SenderID": "12345",
  "botAlias": "iSearchBotBeta",
  "message": {
    "text": "hi"
  }
}

这是我通过 POSTMAN

从 api 网关导入 api 时得到的响应
{
    "ResponseMetadata": {
        "RequestId": "65e1b452-65e4-11e9-ab8a-d328589017aa",
        "HTTPStatusCode": 200,
        "HTTPHeaders": {
            "content-type": "application/json",
            "date": "Tue, 23 Apr 2019 16:25:25 GMT",
            "x-amzn-requestid": "65e1b452-65e4-11e9-ab8a-d328589017aa",
            "content-length": "709",
            "connection": "keep-alive"
        },
        "RetryAttempts": 0
    },
    "intentName": "HotelReservation",
    "slots": {
        "FromDate": null,
        "Location": null,
        "adultCount": null,
        "checkOutDate": null,
        "childCount": null,
        "childExists": null,
        "noOfRooms": null,
        "searchHotel": null,
        "welcome": null
    },
    "sessionAttributes": {},
    "message": "I am iSearchBot,I can help you book a hotel",
    "messageFormat": "PlainText",
    "dialogState": "ElicitSlot",
    "slotToElicit": "welcome",
    "responseCard": {
        "version": "1",
        "contentType": "application/vnd.amazonaws.card.generic",
        "genericAttachments": [
            {
                "title": "Do you want to book a Hotel",
                "imageUrl": "https://pbs.twimg.com/profile_images/1034820690463997957/TZEsJwEa_400x400.jpg",
                "buttons": [
                    {
                        "text": "Yes",
                        "value": "Yes"
                    },
                    {
                        "text": "No",
                        "value": "No"
                    }
                ]
            }
        ]
    }
}

提前致谢任何帮助都会很棒

好的,我不完全了解您的设置,但我会为您指明方向。问题是请求需要用它的原始主体和类型 application/json 来完成。这里的关键是 "Content-Type" 和有效负载 JSON。如果您不使用这个库,我相信另一个库也有类似的选项。

import http.client

conn = http.client.HTTPConnection("ym4j4pt5mf,execute-api,us-east-1,amazonaws,com")

payload = "{\n  \"DestinationBot\": \"iSearchBot\",\n  \"SenderID\": \"12345\",\n  \"botAlias\": \"iSearchBotBeta\",\n  \"message\": {\n    \"text\": \"hi\"\n  }\n}"

headers = {
  'Content-Type': "application/json",
  'cache-control': "no-cache",
  'Postman-Token': "0de52364-daf7-4977-8b82-55d5258a4046"
  }

conn.request("POST", "Beta", payload, headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))

或者如果您使用请求:

import requests

url = "https://ym4j4pt5mf.execute-api.us-east-1.amazonaws.com/Beta"

payload = "{\n  \"DestinationBot\": \"iSearchBot\",\n  \"SenderID\": \"12345\",\n  \"botAlias\": \"iSearchBotBeta\",\n  \"message\": {\n    \"text\": \"hi\"\n  }\n}"
headers = {
    'Content-Type': "application/json",
    'cache-control': "no-cache",
    'Postman-Token': "245fea6e-5604-47dd-96ec-745ae2b6cde0"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)

嗯,这会解决你的问题,我想问题出在 json stringify 上,它很容易工作

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){

var value={ 
  'DestinationBot': "iSearchBot",
  'SenderID': "12345",
  'botAlias': "iSearchBotBeta",
  'message': {
    'text': "hi"
  }
};
value = JSON.stringify(value);
$.ajax({
  url:'https://ym4j4pt5mf.execute-api.us-east-1.amazonaws.com/Beta/',
  headers:{  
    'Content-Type': "application/json",   
  },
  crossDomain: true,
  method:'POST',
  dataType:'json',
  data:value,
  success:function(msg){
    console.log(msg)
  }
});
});

</script>
</head>
<body>

<input type="text"></input>

</body>
</html>