Skip to content
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

Multipart form request with fields of the same name only retains one field value #277

Open
AfzalivE opened this issue May 15, 2019 · 6 comments

Comments

@AfzalivE
Copy link

AfzalivE commented May 15, 2019

With Multipart form data, it is allowed to have multiple fields of the same name with different values and they all are submitted to the server.

A curl example of that is below (from Mailgun API documentation):

curl -s --user 'api:YOUR_API_KEY' \
   https://api.mailgun.net/v3/routes \
   -F priority=0 \
   -F description='Sample route' \
   -F expression='match_recipient(".*@YOUR_DOMAIN_NAME")' \
   -F action='forward("http://myhost.com/messages/")' \
   -F action='stop()'

Here is the test case:

  test('with multiple fields of the same name', () {
    var request = http.MultipartRequest('POST', dummyUrl);
    request.fields['field1'] = 'value1';
    request.fields['field1'] = 'value2';

    expect(request, bodyMatches('''
        --{{boundary}}
        content-disposition: form-data; name="field1"

        value1
        --{{boundary}}
        content-disposition: form-data; name="field1"

        value2
        --{{boundary}}--
        '''));
  });

The above test case fails because we store fields in a Map and so value2 replaces the previously set value1 for field1.

AfzalivE added a commit to AfzalivE/http that referenced this issue May 15, 2019
…art request

Converted the fields Map to List of MapEntry objects
@natebosch
Copy link
Member

Closing as a duplicate of #24

@AfzalivE
Copy link
Author

AfzalivE commented May 28, 2019

@natebosch that one is for headers. This one is for fields. Would be nice to get some feedback on the PR.

@natebosch natebosch reopened this May 28, 2019
@natebosch
Copy link
Member

Does it work to do something like:

    var request = http.MultipartRequest('POST', dummyUrl);
    request.fields['field1[0]'] = 'value1';
    request.fields['field1[1]'] = 'value2';

@josephchenghmlet
Copy link

Any alternative solution?
It seems for iOS - AlamoFire and Android - Retrofit is able to submit multiple files with same field key.

@ganigeorgiev
Copy link

I know that this is an old issue, but @natebosch suggestion will not work for all cases if the server implementation doesn't handle the bracket notation as they will be treated as different keys.

And even for those server implementations that support the above, it still will be limited because you'll have to always provide an array index.

In my opinion tagging the http package with v1.0.0 wasn't well thought out, because currently unless the MultipartRequest.fields type change I'm not sure if this can be fixed (the same apply for the linked headers issue).

@abdullahalamodi
Copy link

I know that this is an old issue, but @natebosch suggestion will not work for all cases if the server implementation doesn't handle the bracket notation as they will be treated as different keys.

And even for those server implementations that support the above, it still will be limited because you'll have to always provide an array index.

In my opinion tagging the http package with v1.0.0 wasn't well thought out, because currently unless the MultipartRequest.fields type change I'm not sure if this can be fixed (the same apply for the linked headers issue).

I really feel bad about this event there are a lot of issues related to the same problem but not solved until now.

using
MultipartFile.fromString();
for each key instead of fields is work
but this definitely is not correct but still work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants