POST request with files and fields in wzzle

Ok, I'll give a little background for a start. I have system A written in CakePHP that handles ads and products, etc. I recently worked on another system written in Laravel, which acts as a self-service tool for realtors to host and manage their real estate listings that are in system A. I am now at the download point of self-service images, maintain the site in system A. I wrote a simple controller action in Cake to process the POST request and save the image file on the server.

http://example.com/image/add

I can send POST requests, upload images and get the right response using REST applications like postman . Everything looks good on the side of system A (Cake).

Now in Laravel’s self-service system, I use Guzzle to send HTTP requests. I filled out a write request with exact fields and files, but I am not getting the same result. The request is accepted by system A, but the image is not added and a random HTML page is returned. If the postman and several other applications receive the same response and functionality, but my request sent to Guzzle is not, I think there is a problem with my buzz request. Here is my buzz 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 a 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 a buzzing request per line, 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 group the characters for the image data.

I ask if anyone can see the problem with my POST request in a buzzing, or if someone has encountered this strange buzzing problem before.

: CakePHP 2.4.1

+4
1

, , data[image], image[data].

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

, , , CakeRequest::$data, , data, CakeRequest::$params['form'].

, ( CakePHP) . :

CakePHP: , request- >

+2

Source: https://habr.com/ru/post/1545134/


All Articles