Incorrect Handling of Multiple Form Files with Same Key in JSON Output
Author: RamDurgaSaiCreated Sep 23, 2024Updated Oct 2, 2024
Labelsincorrect output
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 [email protected] -F [email protected] -F "story=<hugefile.txt" --form-string "name=content" example.com/upload.cgiOutput
{
"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.
Source: curlconverter/curlconverter