Skip to content

Commit f8d7dfe

Browse files
author
Ruben DI BATTISTA
committed
Strip empty fields in MultipartEncoder
This is coherent with what's done in requests
1 parent 1112280 commit f8d7dfe

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

requests_toolbelt/multipart/encoder.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ def _iter_fields(self):
227227
file_name, file_pointer, file_type = v
228228
else:
229229
file_name, file_pointer, file_type, file_headers = v
230+
elif v is None:
231+
continue
230232
else:
231233
file_pointer = v
232234

tests/test_multipart_encoder.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# -*- coding: utf-8 -*-
2-
import unittest
32
import io
3+
import unittest
44

55
import requests
6-
7-
from requests_toolbelt.multipart.encoder import (
8-
CustomBytesIO, MultipartEncoder, FileFromURLWrapper, FileNotSupportedError)
96
from requests_toolbelt._compat import filepost
10-
from . import get_betamax
7+
from requests_toolbelt.multipart.encoder import (
8+
CustomBytesIO,
9+
FileFromURLWrapper,
10+
FileNotSupportedError,
11+
MultipartEncoder,
12+
)
1113

14+
from . import get_betamax
1215

1316
preserve_bytes = {'preserve_exact_body_bytes': True}
1417

@@ -272,6 +275,18 @@ def test_regression_2(self):
272275

273276
assert read_so_far == total_size
274277

278+
def test_empty_files_key(self):
279+
"""Ensure empty `fields` keys are stripped from the request"""
280+
281+
fields = {
282+
"test": "this is a test",
283+
"empty": None
284+
}
285+
286+
m = MultipartEncoder(fields=fields)
287+
288+
assert len(m.parts) == 1
289+
275290
def test_handles_empty_unicode_values(self):
276291
"""Verify that the Encoder can handle empty unicode strings.
277292

0 commit comments

Comments
 (0)