为 Guzzle 6 put() 设置请求正文

Setting request body for Guzzle 6 put()

我为这些 Guzzle put() 调用设置了错误的请求正文。我正在使用相同的令牌毫无问题地对相同的 API 进行 get() 调用。

API 有一个测试环境,这是 cURL 中成功的 put 调用的样子:

curl -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'Authorization: Basic tokenishere=' -d '{ \ 
   "OrganizationCode": "10", \ 
   "EventID": 5524, \ 
   "FunctionID": 321, \ 
   "Description": "Test" \ 
 }' 'https://example.com/api/v1/Functions/10/5524/321'

我的 Guzzle 代码是

use GuzzleHttp\Psr7;
use GuzzleHttp\Exception\RequestException;

$client = new \GuzzleHttp\Client(array("base_uri" => "https://example.com"));

$response = $client->put("/api/v1/Functions/10/".$_SESSION['eventID'].'/'.$functionID, 
    array(
        'headers'   => array(
            'token' => $token,  
            'debug' => true,            
        ), 
        'body' => $data
    )

我也试过了

$request = $client->put("/api/v1/Functions/10/$_SESSION[eventID]/$functionID", array('headers' => array('token' => $token)));
$request->setBody($data);
$response = $request->send();

而且我尝试使用参数名称 json 而不是正文。

我已经验证了 $data 中的 JSON 字符串。

https://guzzle3.readthedocs.io/http-client/request.html 显示了正文是第三个参数的示例:

$request = $client->put('http://httpbin.org/put', array(), 'this is the body');

所以我也在下面尝试了这个但是同样的问题...

$request = $client->put("/api/v1/Functions/10/".$_SESSION['eventID'].'/'.$functionID, array(
                        'headers'   => array(
                            'token' => $token,  
                            'debug' => true,            
                        )), $data);

调试输出是

PUT /api/v1/Functions/10/5524/321 HTTP/1.1
User-Agent: GuzzleHttp/6.3.3 curl/7.52.1 PHP/7.3.6-1+0~20190531112735.39+stretch~1.gbp6131b7
Host: example.com
token: tokenisreallyhere==
debug: 1

{"OrganizationCode":10,"EventID":"5524","FunctionID":"321","Description":"Test"}PUT /api/v1/Functions/10/5524/321 HTTP/1.1
User-Agent: GuzzleHttp/6.3.3 curl/7.52.1 PHP/7.3.6-1+0~20190531112735.39+stretch~1.gbp6131b7
Host: example.com
token: tokenisreallyhere==
debug: 1

{"OrganizationCode":10,"EventID":"5524","FunctionID":"321","Description":"Test"}HTTP/1.1 400 Missing model object in request body.  Check body And header content type.
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
ETag: ""
X-Frame-Options: SAMEORIGIN
Date: Thu, 05 Sep 2019 21:57:06 GMT
Content-Length: 0

我在哪里设置正文?

不推荐使用 form_params 正文

$response = $client->put("/api/v1/Functions/10/".$_SESSION['eventID'].'/'.$functionID, 
    array(
        'headers'   => array(
            'token' => $token,  
            'debug' => true,            
        ), 
        'form_params' => $data
    )

当然我认为无关紧要的部分是答案:我在通过它之前没有表明我在$data上使用了json_encode()。该数组,而不是 json 对象字符串,是预期的,因此使用 $data 作为数组可以正常工作。

$response = $client->put("/api/v1/Functions/10/".$_SESSION['eventID'].'/'.$functionID, 
                    array(
                        'headers'   => array(
                            'token' => $token,  
                            'debug' => true     
                        ), 
                        'json' => $data
                  )
                );