douhuang3740 2015-05-26 08:39
浏览 60

将文件上传到Apigility时“指定了无效的内容类型”

I'm a building a RPC API using Zend Framework 2 and Apigility by Zend Framework. For testing, I use the chrome extension Postman REST-Client.

I can do POST requests without problems when I use Postman. But my code doesn't work.

$client = new \Zend\Http\Client();
$client->setUri($uri)
        ->setMethod('POST')
        ->setParameterPost(
            array(
                'file' => '/home/user/Downloads/file.csv'
            )
        );

$headers = new \Zend\Http\Headers();

$headers->addHeaders(array(
    'Accept' => 'application/json;',
));

$client->setHeaders($headers);

$client->setStream();
$response = $client->send();

$file = '/home/user/Downloads/file.csv';
$file2 = '/home/user/Downloads/file2.csv';
copy($response->getStreamName(), $file);
$fp = fopen($file2, "w");
stream_copy_to_stream($response->getStream(), $fp);
$client->setStream($file);

$responce = $client->send();

echo $responce->getBody();

I tried to pass other headers Content-Type, but it leads to Fatal error. What do I need to pass the headers to make it work?

  • 写回答

1条回答 默认 最新

  • dongsu3654 2015-06-11 07:00
    关注

    You have to set your Content-Type header to multipart/form-data. The Postman add-on you are using in Chrome does this automatically for file uploads.

    So set your headers like this:

    $headers->addHeaders(array(
        'Accept' => 'application/json',
        'Content-Type' => 'multipart/form-data'
    ));
    

    If that doesn't work please be more specific about the fatal error you get.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀