Search code examples
amazon-web-servicesaws-api-gatewayamazon-lex

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


enter image description hereenter image description hereActually everything runs fine when I import the json to my POSTMAN application and send the request .But problem arises when I try to POST using an ajax call or directly hit the api

I had tried to remove api key and made every authorization none

This is my API - https://ym4j4pt5mf.execute-api.us-east-1.amazonaws.com/Beta And I am trying to post this as raw body:-

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

This is the response that I get when I hit the api by importing it from api gateway via POSTMAN

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

Thanks in advance any help will be great


Solution

  • Well this will solve your problem i guess the problem was with json stringify it works easily

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