Junior — Middle
Can you explain what the multipart/form-data format is and what it is usually used for when transmitting data over HTTP?
sobes.tech AI
Answer from AI
The multipart/form-data format is used for transmitting data in HTTP requests when you need to send not just text fields, but also files or binary data along with regular parameters. Unlike the standard application/x-www-form-urlencoded, where all data is encoded into a string, multipart/form-data splits the request body into parts (separated by special boundaries), each containing a separate form field with headers and content.
This allows, for example, sending images, documents, or other files along with text data in a single request.
An example of using it in Flutter with the http package to send a file and parameters:
import 'package:http/http.dart' as http;
var request = http.MultipartRequest('POST', Uri.parse('https://example.com/upload'));
request.fields['username'] = 'user123';
request.files.add(await http.MultipartFile.fromPath('file', '/path/to/file.jpg'));
var response = await request.send();
if (response.statusCode == 200) {
print('Upload successful');
}
Thus, multipart/form-data is a standard for transmitting complex forms with files over HTTP.