我无法将json发布到我的api lambda函数。我收到{“ message”:“ Missing Authentication Token”}

时间:2019-04-23 16:18:10

标签: amazon-web-services aws-api-gateway amazon-lex

enter image description here enter image description here实际上,当我将json导入POSTMAN应用程序并发送请求时,一切运行正常。但是,当我尝试使用ajax调用或直接点击api进行POST时,就会出现问题

我曾尝试删除api密钥,但未做出任何授权

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

{
  "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"
                    }
                ]
            }
        ]
    }
}

在此先感谢您的帮助

2 个答案:

答案 0 :(得分:1)

好的,我不确定您的设置,但我会为您指明方向。问题是请求需要以其原始主体完成,并键入application / json。此处的关键是“ Content-Type”和有效负载JSON。如果您没有完全使用此lib,那么我敢肯定其他人也会有类似的选择。

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)

答案 1 :(得分:1)

好了,这可以解决您的问题,我想问题出在json字符串化上,它很容易工作

<!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>