From 71b18c8b21173cc48326581fe456bf6dc4bef2db Mon Sep 17 00:00:00 2001 From: Mirko Golze Date: Wed, 20 Sep 2023 08:31:23 +0200 Subject: [PATCH] implement fallback to search body language by content-type header --- .../src/utils/importers/postman-collection.js | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/bruno-app/src/utils/importers/postman-collection.js b/packages/bruno-app/src/utils/importers/postman-collection.js index 41af88452..7fd36dad2 100644 --- a/packages/bruno-app/src/utils/importers/postman-collection.js +++ b/packages/bruno-app/src/utils/importers/postman-collection.js @@ -91,7 +91,10 @@ const importPostmanV2CollectionItem = (brunoParent, item) => { } if (bodyMode === 'raw') { - const language = get(i, 'request.body.options.raw.language'); + let language = get(i, 'request.body.options.raw.language'); + if (!language) { + language = searchLanguageByHeader(i.request.header); + } if (language === 'json') { brunoRequestItem.request.body.mode = 'json'; brunoRequestItem.request.body.json = i.request.body.raw; @@ -131,6 +134,21 @@ const importPostmanV2CollectionItem = (brunoParent, item) => { }); }; +const searchLanguageByHeader = (headers) => { + let contentType; + each(headers, (header) => { + if (header.key.toLowerCase() === 'content-type' && !header.disabled) { + if (typeof header.value == 'string' && /^[\w\-]+\/([\w\-]+\+)?json/.test(header.value)) { + contentType = 'json'; + } else if (typeof header.value == 'string' && /^[\w\-]+\/([\w\-]+\+)?xml/.test(header.value)) { + contentType = 'xml'; + } + return false; + } + }); + return contentType; +}; + const importPostmanV2Collection = (collection) => { const brunoCollection = { name: collection.info.name,