From b8b6adfc8c2b1017c728d5f60d8811410fd7d266 Mon Sep 17 00:00:00 2001 From: Stefanus Albert Kosim Date: Sun, 12 Nov 2023 04:02:27 +0800 Subject: [PATCH] allow to upload multipart/form-data --- src/pymeter/api/samplers.py | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/pymeter/api/samplers.py b/src/pymeter/api/samplers.py index 1398ac8..cd49205 100644 --- a/src/pymeter/api/samplers.py +++ b/src/pymeter/api/samplers.py @@ -55,6 +55,22 @@ from pymeter.api.samplers import DummySampler dummy_sampler = DummySampler("dummy_sampler", "hi dummy") +example - 5: +-------------- +Lets send a post request with multipart/form-data: +This post request has a multipart/form-data as a content type, and the body has a single file with `name` being the key and `path/to/file.ext` being the filepath + + + .. code-block:: python + + from pymeter.api import ContentType + from pymeter.api.samplers import HttpSampler + + http_sampler = ( + HttpSampler("echo_get_request", "http://httpbin.org/post") + .post_multipart_formdata("name", "path/to/file.ext", ContentType.MULTIPART_FORM_DATA) + ) + """ import json from typing import Dict, List, Union @@ -150,3 +166,25 @@ def header(self, key: str, value: str) -> Self: raise TypeError("value field must be a string") self._http_sampler_instance = self.java_wrapped_element.header(key, value) return self + + def post_multipart_formdata(self, name: str, filePath: str, content_type: ContentType) -> Self: + """Create a post request sampler + + Args: + + name: the name to be assigned to the file part. + + filePath: path to the file to be sent in the multipart form body. + + content_type (ContentType): the content type of the request + + + Returns: + + Self: a new sampler instance + """ + + self._http_sampler_instance = self.java_wrapped_element.bodyFilePart( + name, filePath, content_type.value + ).method("POST") + return self