我在使用GuzzleHttp在POST请求中传递版本参数时遇到问题。
客户端错误:GET https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone导致错误响应:{“400 Bad Request”: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并添加版本,那么它可以正常工作(好吧,它失败了,因为它在请求中没有要分析的文本):
更改:音调分析器/api/v3/tone
致:音调分析器/api/v3/tone?版本=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'发布于 2018-04-13 20:25:03
需要发送querystring的query参数。请检查以下代码:
$toneAnalyserResponse = $client->request('POST', 'tone-analyzer/api/v3/tone', [
'auth' => ['{username}', '{password}'],
'headers' => [
'Content-Type' => 'text/plain',
],
'query' => [
'version' => '2017-09-21'
]
]);https://stackoverflow.com/questions/49815206
复制相似问题