Fredo
Fredo

Reputation: 11

How to use Symfony HttpClient post data

I want to simulate this CURL command using Symfony 4 HttpClient component :

curl http://localhost/endpoint -F server=123 -F user=me -F fileUrl=@/tmp/test.xml -F reply=1

I tried the Symfony doc sample code but my endpoint do not receive expected result like the raw CURL command...

<%php
use Symfony\Component\HttpClient\HttpClient; // CurlHttpClient in fact

$filepath = '/tmp/test.xml';

$client = HttpClient::create();

$formFields = [
            'server' => '123',
            'user' => 'me',
            'reply' => '1',
            'fileUrl' => DataPart::fromPath($filepath)
        ];
        $formData = new FormDataPart($formFields);


$client->request('POST', 'http://localhost/endpoint', [
    'headers' => $formData->getPreparedHeaders()->toArray(),
    'body' => $formData->bodyToIterable(),
]);

Any idea ? Is my code correct ? Thanks very much

Upvotes: 1

Views: 11781

Answers (0)

Related Questions