簡體   English   中英

缺少參數

[英]Missing parameter

我無法使用 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'

您需要為查詢字符串發送“查詢”參數。 請檢查以下代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM