Ok I'll give a little background to start. I have system A written in CakePHP that handles ads and products and such. Recently I have been working on another system, written in Laravel, that acts as a self-serve tool for realtors to post and manage their real estate listings that reside in system A. I am now at the point of uploading images from the self-serve site to system A. I wrote a simple controller action in Cake to handle a POST request and save the image file on the server.
http://example.com/image/add
I am able to send POST requests, upload images and get a proper response using REST applications such as postman. All looks good on system A (Cake) side of things.
Now in the self-serve system, in Laravel, I am using Guzzle to send HTTP requests. I have filled out the guzzle post request with the exact same fields and files, but I don't recieve the same output. The request is received by system A, but the image is not added and a random HTML page is returned. If postman and a few other applications get the exact same response and functionality, but my request sent in Guzzle is not, I am thinking there is an issue with my guzzle request. Here is my guzzle code:
$client = new Client();
// Create the request.
$request = $client->createRequest("POST", "http://example.com/image/add");
// Set the POST information.
$postBody = $request->getBody();
$postBody->setField('user_id', $userId);
$postBody->setField('api_key', $token);
$postBody->setField('product_id', $product_id);
$postBody->addFile(new PostFile('image[data]', fopen('tmp/images/'.$input->image, 'r')));
// Send the request and get the response.
$response = $client->send($request);
Here is the working POST request from Postman:
POST /image/add HTTP/1.1
Host: example.com
Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="product_id"
1000
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="user_id"
8345
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="api_key"
secretKey
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="data[image]"; filename="steve-jobs.jpg"
Content-Type: image/jpeg
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Here is the guzzle request to string, I apologize for readability issues.
POST /image/add HTTP/1.1 Host: example.com User-Agent: Guzzle/4.0.2 curl/7.22.0 PHP/5.5.9-1+sury.org~precise+1 Content-Length: 124647 Content-Type: multipart/form-data; boundary=53a1f21e04080 --53a1f21e04080 Content-Disposition: form-data; name="user_id" 8345 --53a1f21e04080 Content-Disposition: form-data; name="api_key" secretKey --53a1f21e04080 Content-Disposition: form-data; name="product_id" 1000 --53a1f21e04080 Content-Disposition: form-data; filename="steve-jobs.jpg"; name="image[data]" Content-Type: image/jpeg
Then a bunch a characters for the image data.
I am asking if anyone can see an issue with my POST request in guzzle or if anyone has run into this kind of weird issue with guzzle before.
Edit: I am using CakePHP 2.4.1