Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpClient] multipart/form-data request issue with file name #36738

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

Closed
ghostika opened this issue May 7, 2020 · 2 comments
Closed

[HttpClient] multipart/form-data request issue with file name #36738

ghostika opened this issue May 7, 2020 · 2 comments

Comments

@ghostika
Copy link

ghostika commented May 7, 2020

Symfony version(s) affected: 5.0.7

Description
I have 2 basic SF projects, one is a fileupload microservice and an admin interface for the users. The workflow is that the user first uploads the file in the admin interface and then it makes a post request internally to the microservice.
After some debugging, I realized the problem is with the name of the file.

How to reproduce
The upload controller in the MC is a simple Symfony controller.

public function upload(Request $request)
{
    $uploadedFile = $this->request->files->get('file');
    ....
}

If I call this endpoint with the HTTP client and with a specified filename, the $uploadedFile is null.

The post request with the HTTP client is the same as the example in the Symfony documentation.

public function upload(string $filePath, string $originalFilename): ?string
    {
        $formFields = [
            'file' => DataPart::fromPath($filePath, $originalFilename),
        ];
        $formData = new FormDataPart($formFields);

        $options = [
            'headers' => $formData->getPreparedHeaders()->toArray(),
            'body' => $formData->bodyToIterable(),
        ];

        $response = $this->client->request('POST', self::RESOURCE_PATH, $options);

If the originalFilename is a uuid or it's not specified, everything is ok.

Possible Solution
I can only assume, that the problem is in the HTTP client, but I have no idea where to search for it.

Additional context

Headers:

Content-Disposition: form-data; name=file; filename=58a21f90-ae95-4758-b72b-45f14eddf62d.jpeg

But if I have the original file with the filename: Canon EOS 5DS R (8688 × 5792).JPG

Content-Disposition: form-data; name=file; filename*=utf-8''Canon%20EOS%205DS%20R%20%288688%E2%80%8A%C3%97%E2%80%8A5792%29.JPG

In this case, the $_FILE global variable is simply empty.

@ghostika ghostika added the Bug label May 7, 2020
@ghostika ghostika changed the title [HttpClient] multipart/form-data request issue with bigger file [HttpClient] multipart/form-data request issue with file name May 7, 2020
@wiser
Copy link

wiser commented May 11, 2020

Maybe this issue is also related to the "need" to use double quotes around name and filename in the content-disposition part.
Check samples in this documentation (and related RFCs) : https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

We also talk about this here : #35443 (comment)

@ghostika I use private APIs that do not support file upload requests without theses quotes. Can you please try and confirm that could also solve your issue ? (to test I preg_replace the $formData->bodyToString() result...)

Edit :
To test you can remove the if condition around the first lines of the method getEndOfParameterValue here vendor/symfony/mime/Header/ParameterizedHeader.php so value will always be double-quoted :
$value = '"'.$value.'"';

Thx

@fabpot
Copy link
Member

fabpot commented Jul 15, 2020

Closing as there is no feedback. Probably a duplicate of #37500

@fabpot fabpot closed this as completed Jul 15, 2020
fabpot added a commit that referenced this issue Jul 15, 2020
This PR was merged into the 4.4 branch.

Discussion
----------

[Mime] Fix compat with HTTP requests

| Q             | A
| ------------- | ---
| Branch?       | 4.4 <!-- see below -->
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #37500, Fix #36738, Fix #35443
| License       | MIT
| Doc PR        | n/a

Commits
-------

52e7d7c [Mime] Fix compat with HTTP requests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants