首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >缺少参数

缺少参数
EN

Stack Overflow用户
提问于 2018-04-13 18:46:38
回答 1查看 1.6K关注 0票数 0

我在使用GuzzleHttp在POST请求中传递版本参数时遇到问题。

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

这是我最近尝试过的:

代码语言:javascript
复制
    $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
        ]
    ]);

这也失败了:

代码语言:javascript
复制
$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并添加版本,那么它可以正常工作(好吧,它失败了,因为它在请求中没有要分析的文本):

更改:音调分析器/api/v3/tone

致:音调分析器/api/v3/tone?版本=2017-09-21

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

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

代码语言:javascript
复制
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'
EN

回答 1

Stack Overflow用户

发布于 2018-04-13 20:25:03

需要发送querystring的query参数。请检查以下代码:

代码语言:javascript
复制
$toneAnalyserResponse = $client->request('POST', 'tone-analyzer/api/v3/tone', [
    'auth' => ['{username}', '{password}'],
    'headers' => [
        'Content-Type' => 'text/plain',
    ],
    'query' => [
       'version' => '2017-09-21'
    ]
]);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49815206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档