【问题标题】:API error no parameters when there are parameters given有参数时API错误无参数
【发布时间】:2018-02-28 19:00:03
【问题描述】:

这是我遇到的错误,您可以看到 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;
}

第二次

 $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]);

【问题讨论】:

  • 既然是 PUT 请求,参数不应该作为正文有效负载数据而不是 get 变量提交吗?
  • @user3574492 我该怎么做,我已经在帖子中添加了我的代码。

标签: php laravel parameters


【解决方案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

【讨论】:

  • 仍然不起作用,现在我收到此错误:不推荐将“body”请求选项作为数组传递以发送 POST 请求。请使用“form_params”请求选项发送 application/x-www-form-urlencoded 请求,或使用“multipart”请求选项发送 multipart/form-data 请求。
  • 查看我的更新。试试看。同时发布请求代码。
  • 我已将新代码添加到帖子中,但仍然无法正常工作。
  • 您是否正在尝试将文件发布到服务器?
  • 我正在将设备详细信息从 Teamviewer 设备更新到他们的 api。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-17
相关资源
最近更新 更多