当我将json导入我的POSTMAN应用程序并发送请求时,一切都运行正常。当我尝试使用ajax调用POST或直接点击api时出现问题
我试图删除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"
}
]
}
]
}
}
在此先感谢任何帮助将是伟大的
那么这将解决你的问题我想问题是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>
好的,我不确切知道你的设置,但我会指出你的方向。问题是需要使用它的原始体和类型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)