ello everyone,
I’m encountering an issue with sending multipart/form-data requests in Robot Framework 7.0.1 using the latest version of RequestsLibrary. The problem occurs when the request includes both a large file and additional form fields.
The Scenario:
- I’m using a custom Python function with
requests_toolbelt
to generate the multipart/form-data payload. The function works perfectly when the request includes only the file. - However, when I add additional form fields to the multipart payload and the file size is large (e.g., >10MB), the request fails.
The Problem:
When sending a multipart/form-data request with both a large file and additional form fields, I encounter the following error:
Copy
TypeError: 'MultipartEncoderWithLength' object is not subscriptable
Interestingly, if:
- The payload only includes the file (without form fields), the request succeeds—even for large files.
- The file size is small (e.g., <30KB), the request succeeds even when form fields are included.
What I Have Tried:
- Testing with smaller files: The request works when the file size is small, even if form fields are present.
- Using only the file: The request works for large files if no additional form fields are included in the payload.
- Adjusting headers and buffer sizes: I ensured that
Content-Length
andContent-Type
headers are calculated properly. - Updating libraries: I’m using the latest versions of Robot Framework (7.0.1) and RequestsLibrary.
What I Need Help With:
- Why does the inclusion of form fields alongside a large file cause the multipart request to fail?
- Is there a known limitation in
requests_toolbelt
,RequestsLibrary
, or Robot Framework when handling large multipart requests with form fields? - Are there any workarounds or alternative approaches for sending multipart/form-data with both a large file and form fields in Robot Framework 7.0.1?
Environment Details:
- Robot Framework: 7.0.1
- RequestsLibrary: Latest version (0.10.x as of April 2025)
- requests-toolbelt: 0.10.x
- Python: 3.x
- File Size: >10MB
- Form Fields: Simple key-value pairs (e.g.,
{"name": "example"}
)
Thank you in advance for your help! Any suggestions, insights, or workarounds would be greatly appreciated.
This version includes the updated Robot Framework version and highlights your use of the latest RequestsLibrary. It provides specific details about the problem, making it easier for the community to provide relevant suggestions.
I have
import os
class MultipartEncoderWithLength(MultipartEncoder):
def __len__(self):
return self.len
def create_multipart_data(workflow_file, content_type, name=None, additional_forme=None):
"""
Crée un objet multipart/form-data prêt à être utilisé avec RequestsLibrary pour l'envoi de fichiers
avec des paramètres supplémentaires (multipart encoder direct, avec Content-Length calculé correctement).
*Arguments:*
- ``workflow_file`` : chemin du fichier à envoyer
- ``content_type`` : type MIME du fichier (ex: application/pdf, text/xml)
- ``name`` : nom du fichier dans le champ "name" du formulaire (optionnel)
- ``additional_forme`` : dictionnaire des champs supplémentaires du formulaire (optionnel)
*Retourne:*
- ``multipart_data`` : objet MultipartEncoder avec Content-Length calculé
- ``headers`` : en-têtes HTTP incluant Content-Type et Content-Length
"""
file_name = os.path.basename(workflow_file)
file_stream = open(workflow_file, 'rb')
# Construction des champs pour le formulaire
fields = {
'formFile': (file_name, file_stream, content_type),
'fileName': file_name,
}
if name:
fields['name'] = name
if additional_forme and isinstance(additional_forme, dict):
fields.update(additional_forme)
# Utilisation du wrapper pour le MultipartEncoder
encoder = MultipartEncoderWithLength(fields=fields)
headers = {
'Content-Type': encoder.content_type,
'Content-Length': str(len(encoder))
}
# Retourne un dictionnaire avec multipart_data et headers
return {'multipart_data': encoder, 'headers': headers}```
mon keyword robotframework
import
[Documentation]
[Arguments] ${file_configuration} ${session}
${json}= set variable {"requests":[{"type":"datas","configurationKeys":["c3b2c056-5061-462f-9fbf-0932c3b5f130"],"options":{"withPolicies":true}}]}
${addition_form}= Create Dictionary Action=UpdateExisting Settings=${json}
${Formefile}= Create Multipart Data ${file_configuration} application/zip name=formFile additional_forme=${addition_form}
${Binary_file}= Get Binary File ${file_configuration}
${manifest}= Post on Session ${session} /test data=${Binary_file}
${Reponse}= POST On Session ${session} /test data=${Formefile}[multipart_data] headers=${Formefile}[headers]```
l'erreur appparait apres le POST On Session