|
21 | 21 | import mimetypes |
22 | 22 | import os |
23 | 23 | import re |
| 24 | + |
| 25 | +from urllib3.fields import RequestField |
24 | 26 | import tempfile |
25 | 27 |
|
26 | 28 | from urllib.parse import quote |
@@ -216,6 +218,9 @@ def param_serialize( |
216 | 218 | ) |
217 | 219 | if files: |
218 | 220 | post_params.extend(self.files_parameters(files)) |
| 221 | + if header_params['Content-Type'].startswith("multipart"): |
| 222 | + post_params = self.parameters_to_multipart(post_params, |
| 223 | + (dict)) |
219 | 224 |
|
220 | 225 | # auth setting |
221 | 226 | self.update_params_for_auth( |
@@ -286,6 +291,28 @@ async def call_api( |
286 | 291 |
|
287 | 292 | return response_data |
288 | 293 |
|
| 294 | + def parameters_to_multipart(self, params, collection_types): |
| 295 | + """Get parameters as list of tuples, formatting as json if value is collection_types |
| 296 | +
|
| 297 | + :param params: Parameters as list of two-tuples |
| 298 | + :param dict collection_types: Parameter collection types |
| 299 | + :return: Parameters as list of tuple or urllib3.fields.RequestField |
| 300 | + """ |
| 301 | + new_params = [] |
| 302 | + if collection_types is None: |
| 303 | + collection_types = (dict) |
| 304 | + for k, v in params.items() if isinstance(params, dict) else params: # noqa: E501 |
| 305 | + if isinstance(v, collection_types): # v is instance of collection_type, formatting as application/json |
| 306 | + v = json.dumps(v, ensure_ascii=False).encode("utf-8") |
| 307 | + field = RequestField(k, v) |
| 308 | + field.make_multipart(content_type="application/json; charset=utf-8") |
| 309 | + new_params.append(field) |
| 310 | + elif isinstance(v, str): |
| 311 | + params.append((k, (v, "text/plain; charset=utf-8"))) |
| 312 | + else: |
| 313 | + new_params.append((k, v)) |
| 314 | + return new_params |
| 315 | + |
289 | 316 | def response_deserialize( |
290 | 317 | self, |
291 | 318 | response_data: rest.RESTResponse, |
|
0 commit comments