-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpClient] Multiple form data fields with the same name #38258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Given that multiple parameters with the same name |
@stof Well actually I was able to find a way to achieve this by extending the class NamedTextPart extends TextPart
{
private $isNameSet = false;
public function __construct($name, $body, ?string $charset = 'utf-8', $subtype = 'plain', string $encoding = null)
{
parent::__construct($body, $charset, $subtype, $encoding);
$this->setName($name);
}
public function setName($name)
{
if (!$this->isNameSet) {
parent::setName($name);
$this->isNameSet = true;
}
}
} Even though it's not the prettiest solution, it seems to work. Also, I don't see a reason why the private function prepareFields(array $fields): array
{
// ...
$prepare = function ($item, $key, $root = null) use (&$values, &$prepare) {
$fieldName = $root || $this->someNewFlag ? sprintf('%s[%s]', $root, $key) : $key;
// ...
$values[] = $this->preparePart($fieldName, $item);
};
// ...
return $values;
} All in all it doesn't seem like implementing such a feature would be a problem. |
Could you please send a PR so that we can discuss the proposal on actual code ? |
Will do in the following few days 😄 |
…DataPart (HypeMC) This PR was merged into the 5.2-dev branch. Discussion ---------- [Mime] Allow multiple parts with the same name in FormDataPart | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #38258 | License | MIT | Doc PR | - Added the possibility to choose whether multiple parts with the same name should be suffixed or not: ```php $f1 = new FormDataPart([ 'foo' => [ 'one', 'two', ], ]); var_dump($f1->toString()); ``` ``` Content-Type: multipart/form-data; boundary=6rqazwiG --6rqazwiG Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-Disposition: form-data; name="foo[0]" one --6rqazwiG Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-Disposition: form-data; name="foo[1]" two --6rqazwiG-- ``` ```php $f1 = new FormDataPart([ ['foo' => 'one'], ['foo' => 'two'], ]); var_dump($f1->toString()); ``` ``` Content-Type: multipart/form-data; boundary=xxW9dGzq --xxW9dGzq Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-Disposition: form-data; name="foo" one --xxW9dGzq Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-Disposition: form-data; name="foo" two --xxW9dGzq-- ``` Only applies to numeric keys: ```php $f1 = new FormDataPart([ 'foo' => [ 'a' => 'one', 'b' => 'two', ], ], true); var_dump($f1->toString()); ``` ``` Content-Type: multipart/form-data; boundary=W6qkqgrD --W6qkqgrD Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-Disposition: form-data; name="foo[a]" one --W6qkqgrD Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-Disposition: form-data; name="foo[b]" two --W6qkqgrD-- ``` Commits ------- 1f7f2c6 Allow multiple parts with the same name in FormDataPart
Description
This is related to #33488 but with form data instead of a query string. Basically I need to
POST
multiple values for the same filed. TheFormDataPart
automatically appends[key]
to the filed's name, which in my case does not work as the api I'm using doesn't support this format. It would be great if there was a way to control this behavior and prevent the key from being appended to the name.Example
Actual:
Expected:
The text was updated successfully, but these errors were encountered: