Question

I am using Guzzle in Laravel 4 to send file to the remote server which the server will then process. But while posting the file to the server I'm getting the following exception occurs:

Guzzle \ Http \ Exception \ ClientErrorResponseException
Client error response [status code] 417 [reason phrase] Expectation Failed [url] http://example.com/.....

Following is the code that I am using:

use Guzzle\Service\Client as GuzzleClient;
use Guzzle\Plugin\Cookie\Cookie;
use Guzzle\Plugin\Cookie\CookiePlugin;
use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;

$remote_url = 'http://example.com/';
$client = new GuzzleClient($remote_url);
$client->setSslVerification(FALSE);

$cookieJar = new ArrayCookieJar();
// Create a new cookie plugin
$cookiePlugin = new CookiePlugin($cookieJar);
// Add the cookie plugin to the client
$client->addSubscriber($cookiePlugin);

$post_data = array(
            'username' => $input['username'],
            'password' => $input['password'],
        );
$response = $client->post('login', array(), $post_data)->send();
$response_json = $response->json();
if (isset($response_json['error'])) {
    throw new Exception($response_json['error']);
}

$current_time = date("Y-m-d-H-i-s");
$file = 'C:\test\test_file.zip';
$request = $client
                ->post('receiveFile')
                ->addPostFields(array('current_time'=>$current_time))
                ->addPostFile('file', $file)
                ->send();

The authentication of user seems to work fine and the problem starts only when trying to send the file. The application throws the error only when I'm trying to send the file to the web server. When I try to send the same file to the same application on my local server, I'm getting the results as I expected without any errors.

I looked for similar problems other people might have faced and found one here on SO Posting a file to a web service with Guzzle , but the solution that worked for the OP of that question didn't work for me. What can I do to solve this problem?

Was it helpful?

Solution

It turned out that when sending the request, a Expect header is added to the request. So what I did was remove the Expect header before sending the request, and everything is working as it should. Following is the code that I changed:

$request = $client
            ->post('receiveFile')
            ->addPostFields(array('current_time'=>$current_time))
            ->addPostFile('file', $file)
            ->removeHeader('Expect')
            ->send();

I used the removeHeader method to remove the Expect header. Looks like the removeHeader method must be called just before using the send method, because I had used it before the post method and it hadn't worked before.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top