Amazon Lex postContent错误“无法解码会话属性。”

时间:2018-06-01 10:40:42

标签: php amazon-lex

我正尝试使用AWS SDK for PHP中的postContent将我的网页连接到我的Lex机器人。

我设置凭据和参数然后尝试postContent。以下是相关代码:

$credentials = new \Aws\Credentials\Credentials('XXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXXX');

$args = array(
    'region' => 'us-east-1',
    'version' => 'latest',
    'debug' => true,
    'credentials' => $credentials
);

$lex_client = new Aws\LexRuntimeService\LexRuntimeServiceClient($args);

$lex_response = $lex_client->postContent([
    'accept' => 'text/plain; charset=utf-8',
    'botAlias' => 'XXXX',
    'botName' => 'XXXX',
    'contentType' => 'text/plain; charset=utf-8',
    'inputStream' => $userInput,
    'requestAttributes' => "{}",
    'sessionAttributes' => "{}",
    'userId' => 'XXXXXXXXXXXX',
]);

此错误:

  

'在“https://runtime.lex.us-east-1.amazonaws.com/bot/XXXX/alias/XXXX/user/XXXXXXXXXX/content”上执行“PostContent”时出错;

     

AWS HTTP错误:客户端错误:POST https://runtime.lex.us-east-1.amazonaws.com/bot/XXXX/alias/XXXX/user/XXXXXXXXXX/content导致400 Bad Request响应:   {“message”:“无效请求:无法解码会话属性。会话属性应该是String到String的Base64编码的json映射”}'(长度= 142)

我尝试在sessionAttributes中使用各种JSON字符串,JSON编码字符串和Base64编码字符串,但我仍然会遇到同样的错误。

1 个答案:

答案 0 :(得分:1)

AWS SDK中的LexRuntimeService自动JSON编码 Base64对postContent数组进行编码。通过传递一个JSON字符串,SDK中的json编码会在{}周围放置双引号,使其成为"{}"并导致错误。

因此,只需将sessionAttributesrequestAttributes作为PHP数组传递。

$lex_response = $lex_client->postContent([
    'accept' => 'text/plain; charset=utf-8',
    'botAlias' => 'XXXX',
    'botName' => 'XXXX',
    'contentType' => 'text/plain; charset=utf-8',
    'inputStream' => $userInput,
    'requestAttributes' => array(),
    'sessionAttributes' => array(),            // <---- PHP Array not JSON
    'userId' => 'XXXXXXXXXXXX',
]);