简体   繁体   English

狂饮在多部分请求中发送空数组

[英]Guzzle send empty array in multipart request

I am using Guzzle 6 to send a multipart form request to a 3rd party api(cloud Foundry). 我正在使用Guzzle 6向第三方api(cloud Foundry)发送多部分表单请求。 the api takes 2 parameters "resources" and "application". api接受2个参数“ resources”和“ application”。 here is the doc for the call I am making. 是我打的电话的文档。 in short this deploys a binary to an an applications server. 简而言之,这会将二进制文件部署到应用程序服务器。 below is the code I am using in Guzzle. 以下是我在Guzzle中使用的代码。 I am getting an "invalid resource type" error when trying to send an empty array as the contents of the "resource" parameter. 尝试发送空数组作为“ resource”参数的内容时,出现“无效资源类型”错误。 Guzzle seems to only allow strings here? 食尸鬼似乎只允许在这里串? the api requires that an empty array be sent when a new binary is being pushed. api要求在推送新的二进制文件时发送一个空数组。

here is the code: 这是代码:

 $response = $this->client->put($this->baseUrl . "apps/7887990-654e-4516-8ce9-b37bc2f93a87/bits", [
         'multipart' => [
             [
                 'name' => 'resources',
                 'contents' => []
             ],
             [
                 'name' => 'application',
                 'contents' => '@/tmp/cfdownloadYQfOp7',
             ]
         ]
     ]);

the above fails with the mentioned error, and changing ti to a string results in a bad request to the api. 上面的操作失败并出现上述错误,并且将ti更改为字符串会导致对api的错误请求。

here is the curl command that works properly: 这是curl命令,可以正常工作:

curl -k -X PUT -H "Authorization:token here" -F 'resources=[]' -F "application=@/tmp/cfdownloadF9AxlE" https://api.cloudfoundry.com/v2/apps/2d0f491b-d8dd-4b3a-96f9-58b3678e5dad/bits

does anyone know how to get this to work using the above guzzle code? 有谁知道如何使用上面的枪口代码使它工作?

I have solved this issue. 我已经解决了这个问题。 turns out that it was not am a matter of sending an array, but another error being thrown by guzzle that was masking the real issue. 事实证明,这并不是发送数组的问题,而是由吞吃引发的另一个错误掩盖了实际问题。

first I set guzzle to debug, and turned off exceptions(see below). 首先,我将耗材设置为调试,然后关闭异常(请参见下文)。 Guzzle will mask the actual exceptions from the third party if this is not off. 如果未启用,Guzzle将掩盖来自第三方的实际例外。 I was getting 400 bad response which is correct but it was hiding the actual message which was that the file I was trying to send was not able to be unzipped. 我得到400错误的响应,这是正确的,但它隐藏了实际的消息,即我尝试发送的文件无法解压缩。 I then decided to change the multipart request to use the 'fopen' option from the guzzle docs, rather than using '@' from the cloudfoundry docs. 然后,我决定更改分段请求,以使用耗时文档中的“ fopen”选项,而不是使用cloudfoundry文档中的“ @”。 This solved the problem and all is working fine now. 这解决了问题,现在一切正常。 see belwo for the updated request. 请参阅belwo获取更新的请求。

new Client(['debug'=>true,'exceptions'=>false,'headers' => ['Authorization' => "Bearer " . $token, "Accept" => "application/json"], 'verify' => false])

guzzle request: 食欲要求:

  $response = $this->client->put($this->baseUrl . "apps/cb44bb975-654e-4516-8ce9-b37bc2f93a87/bits", [
         'multipart' => [
             [
                 'name' => 'resources',
                 'contents' => '[]'
             ],
             [
                 'name' => 'application',
                 'contents' => fopen('/tmp/cfdownloadYQfOp7', 'r')
             ]
         ]
     ]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM