给定参数时API错误没有参数

问题描述 投票:0回答:1

这是我得到的错误,因为您可以看到URL中有一个参数,但错误表明没有给出任何参数。有人可以帮帮我吗?

客户端错误:PUT https://webapi.teamviewer.com/api/v1/devices/d38237721?alias=laptop-test导致400 Bad Request响应:{“error”:“invalid_request”,“error_description”:“没有给出参数。”,“error_code”:1}

这是我的代码

public function update($device_id, $options)
{
    $token = 'thereisatokenhere';

    $client = new Client(['base_uri' => 'https://webapi.teamviewer.com/api/v1/']);
    $headers = [
        'Authorization' => 'Bearer ' . $token,
        'Accept-Language' => 'en-US',
        'Content-Type' => 'application/json'
    ];


    $response = $client->request('PUT', 'devices/' . $options['device_id'], [
        'headers'         => $headers,
        'form_params'            => [
            'alias' => $options['alias'],
        ],
    ]);

    $response = json_decode($response->getBody()->getContents(), true);

    $deviceIdsAPI = $response['devices'];

    return $deviceIdsAPI;
}

第2

 $request = new Request('PUT', 'https://webapi.teamviewer.com/api/v1/devices/' . $options['device_id'], ['alias' => $options['alias']]);
        $response = $client->send($request, ['timeout' => 2, 'headers' => $headers]);
php laravel parameters
1个回答
1
投票

以下是Guzzle中PUT请求的示例:

$client->put('devices/' . $options['device_id'], [
    'body'            => [
        'alias' => $options['alias'],
        'other_field' => '123'
    ],
    'headers'         => $headers,
    'allow_redirects' => false,
    'timeout'         => 5
]);

更新:

在最新版本(Guzzle 6)中应该是这样的:

use GuzzleHttp\Psr7\Request;

$request = new Request('PUT', 'http://httpbin.org/put', ['test' => '123']);
$response = $client->send($request, ['timeout' => 2, 'headers' => $headers]);

请参阅this答案,这是官方的Guzzle documentation

© www.soinside.com 2019 - 2024. All rights reserved.