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

Incorrect Handling of Multiple Form Files with Same Key in JSON Output #696

Open
RamDurgaSai opened this issue Sep 23, 2024 · 3 comments · May be fixed by #699
Open

Incorrect Handling of Multiple Form Files with Same Key in JSON Output #696

RamDurgaSai opened this issue Sep 23, 2024 · 3 comments · May be fixed by #699

Comments

@RamDurgaSai
Copy link

When I use the curlconverter to convert a complex curl command to JSON, specifically a command that uploads multiple files using the same form field name (key), the JSON output doesn't capture all the files. Instead, it overwrites the previous file with the last one having the same key.

Example

curl -F files=@picture1.jpg -F files=@picture2.jpg -F "story=<hugefile.txt" --form-string "name=content" example.com/upload.cgi

Output

{
    "url": "http://example.com/upload.cgi",
    "raw_url": "http://example.com/upload.cgi",
    "method": "post",
    "files": {
        "files": "picture2.jpg",
        "story": "hugefile.txt"
    },
    "data": {
        "name": "content"
    }
}

Demonstrating the Problem with JavaScript:

When converting to other languages or libraries, such as JavaScript's Fetch API, multiple files for the same key are handled correctly:

const form = new FormData();
form.append('files', File(['<data goes here>'], 'picture1.jpg'));
form.append('files', File(['<data goes here>'], 'picture2.jpg'));
form.append('story', File(['<data goes here>'], 'hugefile.txt'));
form.append('name', 'content');

fetch('http://example.com/upload.cgi', {
  method: 'POST',
  body: form
});

As illustrated, the correct approach involves using multiple entries for each file under the files key.

@verhovsky
Copy link
Member

What are you using the JSON format for? Because if you used a JSON parser on this

{
    "files": {
        "files": "picture1.jpg",
        "files": "picture2.jpg",
        "story": "hugefile.txt"
    }
}

it would also just ignore the "files": "picture1.jpg", line. But I agree that it would be better to warn about this situation or generate JSON with a duplicate key than silently dropping a key.

@RamDurgaSai
Copy link
Author

I'm utilizing this in a No Code API Testing platform.

@RamDurgaSai
Copy link
Author

Rather than having duplicate keys in the JSON—since all JSON parsers override them—I believe it is better to use an array for that key instead of a string.

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

Successfully merging a pull request may close this issue.

2 participants