1

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?

Wilt
  • 41,477
  • 12
  • 152
  • 203
watlf
  • 11
  • 4
  • What is the fatal error? – dualmon May 27 '15 at 07:13
  • It looks like you're trying to do a file upload. Have you read this? https://apigility.org/documentation/recipes/upload-files-to-api – dualmon Jun 10 '15 at 23:26
  • You have a typo in your variable `$response` (`$responce`) and you should not add a `;` at the end of your `Accept` header value. – Wilt Jun 11 '15 at 07:03

1 Answers1

0

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.

Wilt
  • 41,477
  • 12
  • 152
  • 203