guzzle / guzzle-services

Provides an implementation of the Guzzle Command library that uses Guzzle service descriptions to describe web services, serialize requests, and parse responses into easy to use model structures.
MIT License
253 stars 78 forks source link

Unable to POST multiple multipart parameters #123

Closed arjanvdbos closed 7 years ago

arjanvdbos commented 7 years ago

It's seems that POST-ing multiple multipart paramaters is not possible; the GuzzleHttp\Command\Guzzle\RequestLocation\MultiPartLocation overwrites the body for every parameter. So we end up with only the last parameter in the body.

use GuzzleHttp\Client;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Command\Guzzle\Description;

$client = new Client();
$description = new Description([
    'baseUri' => 'http://httpbin.org/',
    'operations' => [
        'testing' => [
            'httpMethod' => 'POST',
            'uri' => '/post',
            'responseModel' => 'postResponse',
            'parameters' => [
                'foo' => [
                    'type' => 'string',
                    'location' => 'multipart'
                ],
                'baz' => [
                    'type' => 'string',
                    'location' => 'multipart'
                ]
            ]
        ]
    ],
    'models' => [
        'postResponse' => [
            'type' => 'object',
            'additionalProperties' => [
                'location' => 'json'
            ]
        ]
    ]
]);

$guzzleClient = new GuzzleClient($client, $description);

$result = $guzzleClient->testing(['foo' => 'bar', 'baz' => 'qux']);
echo (string) $result;

Will result in:

GuzzleHttp\Command\Result Object
(
    [args] => Array
        (
        )
    [data] => 
    [files] => Array
        (
        )
    [form] => Array
        (
            [baz] => qux
        )
    [headers] => Array
        (
            [Content-Length] => 106
            [Content-Type] => multipart/form-data; boundary=585a331a19fcb
            [Host] => httpbin.org
            [User-Agent] => GuzzleHttp/6.2.1 curl/7.49.1 PHP/7.1.0
        )
    [json] => 
    [origin] => 127.0.0.1
    [url] => http://httpbin.org/post
)
Konafets commented 7 years ago

Can you try latest develop branch. In case you want to send a file use any as param type.

arjanvdbos commented 7 years ago

Thanks man, this solves the issue!