dongwen4487 2015-06-11 14:36
浏览 38
已采纳

如何在BuzzBrowser后调用中将文件数据作为post参数发送

I am using Buzz Browser for calling POST method. I am receiving a file from client at one of the end point.

$fileObj = $requestObject->files->get('image_data');

This is correct fileObject. I have verified this using is_file() method.

Now, I have to fire a Buzz Browser post event using this file as parameter.

$headers = array('Content-Type'=>'multipart/form-data'); 
$url = $genericHost . $api; //A valid URL
$params = array('image_data' => $fileObj);//fileObj obtained above

Posting this info using buzzBrowser

$browser->post($url, $headers, $params);

And receiving it as-

$fileObj = $this->get('request')->files->get('image_data');

But, the final $fileObj is NULL.

I tried to dump the $this->get('request')->files-

object(Symfony\Component\HttpFoundation\FileBag)#11 (1) {
["parameters":protected]=>
     array(0) {
    }
}

Means no file is there. What is wrong in my approach?

Edit:1

After debugging more, I found that file object is being sent as normal parameters:

["request"]=>
  object(Symfony\Component\HttpFoundation\ParameterBag)#7 (1) {
    ["parameters":protected]=>
    array(1) {
      ["image_data"]=>
      string(14) "/tmp/php6QLezs"
    }
  }

The header of the request is:

["headers"]=>
   object(Symfony\Component\HttpFoundation\HeaderBag)#13 (2) {
    ["headers":protected]=>
    array(7) {
      ["content-type"]=>
      array(1) {
        [0]=>
        string(70) "multipart/form-data; boundary=----------------------------1b3e33ff2ecb"
      }
      ["content-length"]=>
      array(1) {
        [0]=>
        string(3) "159"
       }
      ["host"]=>
      array(1) {
        [0]=>
        string(16) "host_name"
      }
      ["accept"]=>
        array(1) {
       [0]=>
       string(3) "*/*"
     }
     ["x-php-ob-level"]=>
      array(1) {
       [0]=>
       int(1)
     }
    }
  • 写回答

1条回答 默认 最新

  • doufei7516 2015-06-12 12:16
    关注

    Finally solved it by using Buzz Browser's send method.

    use Buzz\Message\Form\FormRequest;
    use Buzz\Message\Form\FormUpload; 
    
    $request = new FormRequest();
    $request->setField('image_data', new FormUpload($imageFilePath));
    $request->setHeaders($headers);
    $request->setMethod($method);
    $request->setHost($genericHost);
    $request->setResource($api);
    $response= $this->browser->send($request, null);
    

    It is still not solved using post of Buzz Browser

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建