【问题标题】:Amazon Lex postContent error "Failed to decode Session attributes."Amazon Lex postContent 错误“无法解码会话属性”。
【发布时间】:2018-06-01 10:40:41
【问题描述】:

我正在尝试使用 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":"无效请求:无法解码会话属性。会话属性应该是字符串到字符串的 Base64 编码 json 映射"}' (length=142)

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

【问题讨论】:

  • 您是否尝试将 Content-type 设置为 application/json ?
  • @ParrettApps 感谢您的建议,值得一试,但是:Error="Unsupported content type application/json"

标签: php amazon-lex


【解决方案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',
]);

【讨论】:

    猜你喜欢
    • 2018-11-27
    • 2017-11-15
    • 2013-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2018-11-06
    • 1970-01-01
    相关资源
    最近更新 更多