【问题标题】:Missing parameter缺少参数
【发布时间】:2018-09-23 16:59:18
【问题描述】:

我无法使用 GuzzleHttp 在 POST 请求中传递版本参数。

客户端错误:GET https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone 导致 400 Bad Request 响应:{"code":400,"sub_code":"C00005","error":"URL 中缺少次要版本参数。要使用最新版本版本,添加th(截断...)

这是我最近尝试过的:

    $client = new \GuzzleHttp\Client([
        'base_uri' => 'https://gateway.watsonplatform.net/'
    ]);
    $toneAnalyserResponse = $client->request('POST', 'tone-analyzer/api/v3/tone', [
        'auth' => ['{username}', '{password}'],
        'headers' => [
            'Content-Type' => 'text/plain',
        ],
        'form_params' => [
            'version' => '2017-09-21',
            'tone_input' => $text,
            'sentences' => true
        ]
    ]);

这也失败了:

$client = new \GuzzleHttp\Client([
    'base_uri' => 'https://gateway.watsonplatform.net/'
]);
$toneAnalyserResponse = $client->request('POST', 'tone-analyzer/api/v3/tone', [
    'auth' => ['{username}', '{password}'],
    'headers' => [
        'Content-Type' => 'text/plain',
    ],
    'version' => '2017-09-21',
    'tone_input' => $text,
    'sentences' => true
]);

如果使用 GET 而不是 POST,也会失败。

如果我更改 URI 并添加版本,那么它工作正常(好吧,它失败了,因为它没有在请求中分析的文本):

更改:tone-analyzer/api/v3/tone

收件人:tone-analyzer/api/v3/tone?version=2017-09-21

所以,我想问题是如何在使用 GuzzleHttp 的请求中传递 URL 参数?

注意:我的 CURL 命令工作正常(http 200):

curl -X POST -u "{username}":"{password}" --header 'Content-Type: text/plain' --header 'Accept: application/json' --header 'Content-Language: en' --header 'Accept-Language: en' -d 'I hate these new features On #ThisPhone after the update.\r\n \ 
     I hate #ThisPhoneCompany products, you%27d have to torture me to get me to use #ThisPhone.\r\n \ 
     The emojis in #ThisPhone are stupid.\r\n \ 
     #ThisPhone is a useless, stupid waste of money.\r\n \ 
     #ThisPhone is the worst phone I%27ve ever had - ever ????.\r\n \ 
     #ThisPhone another ripoff, lost all respect SHAME.\r\n \ 
     I%27m worried my #ThisPhone is going to overheat like my brother%27s did.\r\n \ 
     #ThisPhoneCompany really let me down... my new phone won%27t even turn on.' 'https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21&sentences=true'

【问题讨论】:

    标签: php laravel guzzle


    【解决方案1】:

    您需要为查询字符串发送“查询”参数。请检查以下代码:

    $toneAnalyserResponse = $client->request('POST', 'tone-analyzer/api/v3/tone', [
        'auth' => ['{username}', '{password}'],
        'headers' => [
            'Content-Type' => 'text/plain',
        ],
        'query' => [
           'version' => '2017-09-21'
        ]
    ]);
    

    【讨论】:

    • 谢谢。我的声望不到 15,所以不能给你“高五”。
    猜你喜欢
    • 1970-01-01
    • 2022-01-16
    • 2012-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多