From b80f6e2b0a8df6bcda337eb91186638b813a883b Mon Sep 17 00:00:00 2001 From: Alar Floren Date: Thu, 12 Oct 2023 14:29:54 +0300 Subject: [PATCH] 223 modification to multipart file sending DSL syntax --- samples/steps/http-post.md | 7 ++++--- src/main/java/ee/buerokratt/ruuter/helper/HttpHelper.java | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/samples/steps/http-post.md b/samples/steps/http-post.md index 8b0d91d1..3e81e416 100644 --- a/samples/steps/http-post.md +++ b/samples/steps/http-post.md @@ -33,8 +33,9 @@ post_step: * `contentType` - specifies the contenttype to use, currently allowed values: * `"plaintext"` - uses field `plaintext` and mediaType 'text/plain' * `"formdata"` - - if any field names start with `file:`, that field is sent as a file with -filename field name without `file:` part and mediatype "multipart/form-data"; + - if a key start with `file:`, that field content is sent as a file on a +field named as the second part of the key and original filename as third part of +the key, for example `file:projectdata:Project.csv`, and mediatype "multipart/form-data"; - otherwise maps `body` as url-encoded form and mediatype 'application/x-www-form-urlencoded' as * If left empty, `body` is posted as JSON and 'application/json' is used as mediatype. @@ -89,7 +90,7 @@ post_step: contentType: formdata body: description: "Requested file" - file:requested.txt: > + file:fieldname:requested.txt: > This is the required content formatted as YAML multiline string result: the_message diff --git a/src/main/java/ee/buerokratt/ruuter/helper/HttpHelper.java b/src/main/java/ee/buerokratt/ruuter/helper/HttpHelper.java index 7da4df06..622ee277 100644 --- a/src/main/java/ee/buerokratt/ruuter/helper/HttpHelper.java +++ b/src/main/java/ee/buerokratt/ruuter/helper/HttpHelper.java @@ -86,8 +86,9 @@ public ResponseEntity doMethod(HttpMethod method, for (Map.Entry e : body.entrySet()) { if (e.getKey().startsWith("file:")) { byte[] bytes = ((String) e.getValue()).getBytes(); - String filename= e.getKey().replace("file:", ""); - builder.part(e.getKey(), new ByteArrayResource(bytes)).filename(filename); + String fieldname = e.getKey().split(":")[1]; + String filename = e.getKey().split(":")[2]; + builder.part(fieldname, new ByteArrayResource(bytes)).filename(filename); } else { builder.part(e.getKey(), e.getValue());