4

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

user3753755
  • 73
  • 1
  • 6

1 Answers1

2

As far as I can tell it's just a small mistake in the file field name, it should be data[image], not image[data].

new PostFile('data[image]', /* ... */);

In your app you are probably relying on the file data being available via CakeRequest::$data, however it will only land there in case it's wrapped in the data key, otherwise it is added to CakeRequest::$params['form'].

Not really a duplicate, however for (a more CakePHP form helper related) reference see also:

CakePHP: posted file data not included in request->data

Community
  • 1
  • 1
ndm
  • 59,784
  • 9
  • 71
  • 110
  • Thank you for your answer; that is an issue and I fixed it. But, my problem still persists in that I am not getting the proper response. – user3753755 Jun 18 '14 at 21:34
  • @user3753755 You'll have to add some more info in order to be able to tell what might be the problem, for example the exact response you are receiving, your related CakePHP controller code, and possible debugging results (like what data exactly is available in CakePHP for your shown request). – ndm Jun 18 '14 at 21:42