Description
Symfony version(s) affected: 4.4.10
Description
I try to upload some data (regular field and/or file field) with the Http Client and so I use the Symfony Mime component to produce the request body. With some APIs it works but not with all...
So I've lean over this behaviour looking for explanation and I've discover that when the name of the field does not contain spaces or other "strange chars", it is not enclosed with quotes.
I use several private APIs from market products (like Marklogic, Dalim Twist) which does not understand multipart form requests without quotes around fields names, even if these fields has simple names as 'field'...
How to reproduce
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
$formFields = [
'regular_field' => 'some value',
'application/xml' => 'some other value'
];
$formData = new FormDataPart($formFields);
dump($formData->bodyToString());
If you check the generated body you can see that the "regular_field" is not enclosed by quotes :
Content-Disposition: form-data; name=regular_field
But the other field with the name containing a slash appears with quotes :
Content-Disposition: form-data; name="application/xml"
Possible Solution
I can’t change fields names of requests because APIs I call does not belong to me.
So my first idea was to preg_replace generated body to add the quotes but some of them contains files and their length can be very high.
So I've check the code in Symfony which produces the body and all the magic is here :
namespace Symfony\Component\Mime\Header\ParameterizedHeader::getEndOfParameterValue()
in line 161.
const TOKEN_REGEX = '(?:[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E]+)';
if (!preg_match('/^'.self::TOKEN_REGEX.'$/D', $value)) {
$value = '"'.$value.'"';
}
Can you please point me a way to manage this issue ?
Can I override this behaviour with some tricks ?
Maybe this library can be changed to always add quotes arround values ?
My last chance is to change the whole HTTP library but according to me the Symfony one is the more convenient to use.
Thank you for your help.
BR.
Wiser