mirror of
https://github.com/usebruno/bruno.git
synced 2025-01-09 15:38:34 +01:00
feature: Multi-part requests: user should be able to set content-type for each part in a multi-part request. #1602
This commit is contained in:
parent
d027d90ed5
commit
39f60daca7
@ -24,7 +24,7 @@ const Wrapper = styled.div`
|
|||||||
width: 30%;
|
width: 30%;
|
||||||
}
|
}
|
||||||
|
|
||||||
&:nth-child(3) {
|
&:nth-child(4) {
|
||||||
width: 70px;
|
width: 70px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,6 +52,10 @@ const MultipartFormParams = ({ item, collection }) => {
|
|||||||
param.value = e.target.value;
|
param.value = e.target.value;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case 'contentType': {
|
||||||
|
param.contentType = e.target.value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
case 'enabled': {
|
case 'enabled': {
|
||||||
param.enabled = e.target.checked;
|
param.enabled = e.target.checked;
|
||||||
break;
|
break;
|
||||||
@ -83,6 +87,7 @@ const MultipartFormParams = ({ item, collection }) => {
|
|||||||
<tr>
|
<tr>
|
||||||
<td>Key</td>
|
<td>Key</td>
|
||||||
<td>Value</td>
|
<td>Value</td>
|
||||||
|
<td>Content-Type</td>
|
||||||
<td></td>
|
<td></td>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
@ -142,6 +147,26 @@ const MultipartFormParams = ({ item, collection }) => {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</td>
|
</td>
|
||||||
|
<td>
|
||||||
|
<MultiLineEditor
|
||||||
|
onSave={onSave}
|
||||||
|
theme={storedTheme}
|
||||||
|
value={param.contentType}
|
||||||
|
onChange={(newValue) =>
|
||||||
|
handleParamChange(
|
||||||
|
{
|
||||||
|
target: {
|
||||||
|
value: newValue
|
||||||
|
}
|
||||||
|
},
|
||||||
|
param,
|
||||||
|
'contentType'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
onRun={handleRun}
|
||||||
|
collection={collection}
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<div className="flex items-center">
|
<div className="flex items-center">
|
||||||
<input
|
<input
|
||||||
|
@ -625,6 +625,7 @@ export const collectionsSlice = createSlice({
|
|||||||
name: '',
|
name: '',
|
||||||
value: '',
|
value: '',
|
||||||
description: '',
|
description: '',
|
||||||
|
contentType: '',
|
||||||
enabled: true
|
enabled: true
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -646,6 +647,7 @@ export const collectionsSlice = createSlice({
|
|||||||
param.name = action.payload.param.name;
|
param.name = action.payload.param.name;
|
||||||
param.value = action.payload.param.value;
|
param.value = action.payload.param.value;
|
||||||
param.description = action.payload.param.description;
|
param.description = action.payload.param.description;
|
||||||
|
param.contentType = action.payload.param.contentType;
|
||||||
param.enabled = action.payload.param.enabled;
|
param.enabled = action.payload.param.enabled;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,38 @@
|
|||||||
const { get, each, filter } = require('lodash');
|
const { get, each, filter, extend } = require('lodash');
|
||||||
const fs = require('fs');
|
|
||||||
var JSONbig = require('json-bigint');
|
|
||||||
const decomment = require('decomment');
|
const decomment = require('decomment');
|
||||||
|
var JSONbig = require('json-bigint');
|
||||||
|
const FormData = require('form-data');
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
const parseFormData = (datas, collectionPath) => {
|
||||||
|
// make axios work in node using form data
|
||||||
|
// reference: https://github.com/axios/axios/issues/1006#issuecomment-320165427
|
||||||
|
const form = new FormData();
|
||||||
|
datas.forEach((item) => {
|
||||||
|
const value = item.value;
|
||||||
|
const name = item.name;
|
||||||
|
let options = {};
|
||||||
|
if (item.contentType) {
|
||||||
|
options.contentType = item.contentType;
|
||||||
|
}
|
||||||
|
if (item.type === 'file') {
|
||||||
|
const filePaths = value || [];
|
||||||
|
filePaths.forEach((filePath) => {
|
||||||
|
let trimmedFilePath = filePath.trim();
|
||||||
|
|
||||||
|
if (!path.isAbsolute(trimmedFilePath)) {
|
||||||
|
trimmedFilePath = path.join(collectionPath, trimmedFilePath);
|
||||||
|
}
|
||||||
|
options.filename = path.basename(trimmedFilePath);
|
||||||
|
form.append(name, fs.createReadStream(trimmedFilePath), options);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
form.append(name, value, options);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return form;
|
||||||
|
};
|
||||||
|
|
||||||
const prepareRequest = (request, collectionRoot) => {
|
const prepareRequest = (request, collectionRoot) => {
|
||||||
const headers = {};
|
const headers = {};
|
||||||
@ -124,17 +155,11 @@ const prepareRequest = (request, collectionRoot) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (request.body.mode === 'multipartForm') {
|
if (request.body.mode === 'multipartForm') {
|
||||||
const params = {};
|
|
||||||
const enabledParams = filter(request.body.multipartForm, (p) => p.enabled);
|
const enabledParams = filter(request.body.multipartForm, (p) => p.enabled);
|
||||||
each(enabledParams, (p) => {
|
const collectionPath = process.cwd();
|
||||||
if (p.type === 'file') {
|
const form = parseFormData(enabledParams, collectionPath);
|
||||||
params[p.name] = p.value.map((path) => fs.createReadStream(path));
|
extend(axiosRequest.headers, form.getHeaders());
|
||||||
} else {
|
axiosRequest.data = form;
|
||||||
params[p.name] = p.value;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
axiosRequest.headers['content-type'] = 'multipart/form-data';
|
|
||||||
axiosRequest.data = params;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.body.mode === 'graphql') {
|
if (request.body.mode === 'graphql') {
|
||||||
|
@ -12,6 +12,10 @@ const parseFormData = (datas, collectionPath) => {
|
|||||||
datas.forEach((item) => {
|
datas.forEach((item) => {
|
||||||
const value = item.value;
|
const value = item.value;
|
||||||
const name = item.name;
|
const name = item.name;
|
||||||
|
let options = {};
|
||||||
|
if (item.contentType) {
|
||||||
|
options.contentType = item.contentType;
|
||||||
|
}
|
||||||
if (item.type === 'file') {
|
if (item.type === 'file') {
|
||||||
const filePaths = value || [];
|
const filePaths = value || [];
|
||||||
filePaths.forEach((filePath) => {
|
filePaths.forEach((filePath) => {
|
||||||
@ -20,11 +24,11 @@ const parseFormData = (datas, collectionPath) => {
|
|||||||
if (!path.isAbsolute(trimmedFilePath)) {
|
if (!path.isAbsolute(trimmedFilePath)) {
|
||||||
trimmedFilePath = path.join(collectionPath, trimmedFilePath);
|
trimmedFilePath = path.join(collectionPath, trimmedFilePath);
|
||||||
}
|
}
|
||||||
|
options.filename = path.basename(trimmedFilePath);
|
||||||
form.append(name, fs.createReadStream(trimmedFilePath), path.basename(trimmedFilePath));
|
form.append(name, fs.createReadStream(trimmedFilePath), options);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
form.append(name, value);
|
form.append(name, value, options);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return form;
|
return form;
|
||||||
|
@ -45,7 +45,7 @@ const grammar = ohm.grammar(`Bru {
|
|||||||
pair = st* key st* ":" st* value st*
|
pair = st* key st* ":" st* value st*
|
||||||
key = keychar*
|
key = keychar*
|
||||||
value = multilinetextblock | valuechar*
|
value = multilinetextblock | valuechar*
|
||||||
|
|
||||||
// Dictionary for Assert Block
|
// Dictionary for Assert Block
|
||||||
assertdictionary = st* "{" assertpairlist? tagend
|
assertdictionary = st* "{" assertpairlist? tagend
|
||||||
assertpairlist = optionalnl* assertpair (~tagend stnl* assertpair)* (~tagend space)*
|
assertpairlist = optionalnl* assertpair (~tagend stnl* assertpair)* (~tagend space)*
|
||||||
@ -133,16 +133,31 @@ const mapPairListToKeyValPairs = (pairList = [], parseEnabled = true) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const multipartExtractContentType = (pair) => {
|
||||||
|
if (_.isString(pair.value)) {
|
||||||
|
const match = pair.value.match(/^(.*?)\s*\(Content-Type=(.*?)\)\s*$/);
|
||||||
|
if (match != null && match.length > 2) {
|
||||||
|
pair.value = match[1];
|
||||||
|
pair.contentType = match[2];
|
||||||
|
} else {
|
||||||
|
pair.contentType = '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const mapPairListToKeyValPairsMultipart = (pairList = [], parseEnabled = true) => {
|
const mapPairListToKeyValPairsMultipart = (pairList = [], parseEnabled = true) => {
|
||||||
const pairs = mapPairListToKeyValPairs(pairList, parseEnabled);
|
const pairs = mapPairListToKeyValPairs(pairList, parseEnabled);
|
||||||
|
|
||||||
return pairs.map((pair) => {
|
return pairs.map((pair) => {
|
||||||
pair.type = 'text';
|
pair.type = 'text';
|
||||||
|
multipartExtractContentType(pair);
|
||||||
|
|
||||||
if (pair.value.startsWith('@file(') && pair.value.endsWith(')')) {
|
if (pair.value.startsWith('@file(') && pair.value.endsWith(')')) {
|
||||||
let filestr = pair.value.replace(/^@file\(/, '').replace(/\)$/, '');
|
let filestr = pair.value.replace(/^@file\(/, '').replace(/\)$/, '');
|
||||||
pair.type = 'file';
|
pair.type = 'file';
|
||||||
pair.value = filestr.split('|');
|
pair.value = filestr.split('|');
|
||||||
}
|
}
|
||||||
|
|
||||||
return pair;
|
return pair;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -247,16 +247,18 @@ ${indentString(body.sparql)}
|
|||||||
multipartForms
|
multipartForms
|
||||||
.map((item) => {
|
.map((item) => {
|
||||||
const enabled = item.enabled ? '' : '~';
|
const enabled = item.enabled ? '' : '~';
|
||||||
|
const contentType =
|
||||||
|
item.contentType && item.contentType !== '' ? ' (Content-Type=' + item.contentType + ')' : '';
|
||||||
|
|
||||||
if (item.type === 'text') {
|
if (item.type === 'text') {
|
||||||
return `${enabled}${item.name}: ${item.value}`;
|
return `${enabled}${item.name}: ${item.value}${contentType}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item.type === 'file') {
|
if (item.type === 'file') {
|
||||||
let filepaths = item.value || [];
|
let filepaths = item.value || [];
|
||||||
let filestr = filepaths.join('|');
|
let filestr = filepaths.join('|');
|
||||||
const value = `@file(${filestr})`;
|
const value = `@file(${filestr})`;
|
||||||
return `${enabled}${item.name}: ${value}`;
|
return `${enabled}${item.name}: ${value}${contentType}`;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.join('\n')
|
.join('\n')
|
||||||
|
@ -103,18 +103,21 @@
|
|||||||
],
|
],
|
||||||
"multipartForm": [
|
"multipartForm": [
|
||||||
{
|
{
|
||||||
|
"contentType": "",
|
||||||
"name": "apikey",
|
"name": "apikey",
|
||||||
"value": "secret",
|
"value": "secret",
|
||||||
"enabled": true,
|
"enabled": true,
|
||||||
"type": "text"
|
"type": "text"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"contentType": "",
|
||||||
"name": "numbers",
|
"name": "numbers",
|
||||||
"value": "+91998877665",
|
"value": "+91998877665",
|
||||||
"enabled": true,
|
"enabled": true,
|
||||||
"type": "text"
|
"type": "text"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"contentType": "",
|
||||||
"name": "message",
|
"name": "message",
|
||||||
"value": "hello",
|
"value": "hello",
|
||||||
"enabled": false,
|
"enabled": false,
|
||||||
|
@ -68,6 +68,7 @@ const multipartFormSchema = Yup.object({
|
|||||||
otherwise: Yup.string().nullable()
|
otherwise: Yup.string().nullable()
|
||||||
}),
|
}),
|
||||||
description: Yup.string().nullable(),
|
description: Yup.string().nullable(),
|
||||||
|
contentType: Yup.string().nullable(),
|
||||||
enabled: Yup.boolean()
|
enabled: Yup.boolean()
|
||||||
})
|
})
|
||||||
.noUnknown(true)
|
.noUnknown(true)
|
||||||
|
@ -0,0 +1,39 @@
|
|||||||
|
meta {
|
||||||
|
name: mixed-content-types
|
||||||
|
type: http
|
||||||
|
seq: 1
|
||||||
|
}
|
||||||
|
|
||||||
|
post {
|
||||||
|
url: {{host}}/api/multipart/mixed-content-types
|
||||||
|
body: multipartForm
|
||||||
|
auth: none
|
||||||
|
}
|
||||||
|
|
||||||
|
body:multipart-form {
|
||||||
|
param1: test
|
||||||
|
param2: {"test":"i am json"} (Content-Type=application/json)
|
||||||
|
param3: @file(multipart/small.png)
|
||||||
|
}
|
||||||
|
|
||||||
|
tests {
|
||||||
|
test("Status code is 200", function () {
|
||||||
|
expect(res.getStatus()).to.equal(200);
|
||||||
|
});
|
||||||
|
test("param1 has no content-type", function () {
|
||||||
|
var param1 = res.body.find(p=>p.name === 'param1')
|
||||||
|
expect(param1).to.be.an('object');
|
||||||
|
expect(param1.contentType).to.be.undefined;
|
||||||
|
});
|
||||||
|
test("param2 has content-type application/json", function () {
|
||||||
|
var param2 = res.body.find(p=>p.name === 'param2')
|
||||||
|
expect(param2).to.be.an('object');
|
||||||
|
expect(param2.contentType).to.equals('application/json');
|
||||||
|
});
|
||||||
|
test("param3 has content-type image/png", function () {
|
||||||
|
var param3 = res.body.find(p=>p.name === 'param3')
|
||||||
|
expect(param3).to.be.an('object');
|
||||||
|
expect(param3.contentType).to.equals('image/png');
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
BIN
packages/bruno-tests/collection/multipart/small.png
Normal file
BIN
packages/bruno-tests/collection/multipart/small.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 132 B |
@ -2,23 +2,25 @@ const express = require('express');
|
|||||||
const bodyParser = require('body-parser');
|
const bodyParser = require('body-parser');
|
||||||
const xmlparser = require('express-xml-bodyparser');
|
const xmlparser = require('express-xml-bodyparser');
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
const multer = require('multer');
|
const formDataParser = require('./multipart/form-data-parser');
|
||||||
|
|
||||||
const app = new express();
|
const app = new express();
|
||||||
const port = process.env.PORT || 8080;
|
const port = process.env.PORT || 8080;
|
||||||
const upload = multer();
|
|
||||||
|
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(xmlparser());
|
app.use(xmlparser());
|
||||||
app.use(bodyParser.text());
|
app.use(bodyParser.text());
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
app.use(bodyParser.urlencoded({ extended: true }));
|
app.use(bodyParser.urlencoded({ extended: true }));
|
||||||
|
formDataParser.init(app, express);
|
||||||
|
|
||||||
const authRouter = require('./auth');
|
const authRouter = require('./auth');
|
||||||
const echoRouter = require('./echo');
|
const echoRouter = require('./echo');
|
||||||
|
const multipartRouter = require('./multipart');
|
||||||
|
|
||||||
app.use('/api/auth', authRouter);
|
app.use('/api/auth', authRouter);
|
||||||
app.use('/api/echo', echoRouter);
|
app.use('/api/echo', echoRouter);
|
||||||
|
app.use('/api/multipart', multipartRouter);
|
||||||
|
|
||||||
app.get('/ping', function (req, res) {
|
app.get('/ping', function (req, res) {
|
||||||
return res.send('pong');
|
return res.send('pong');
|
||||||
@ -32,10 +34,6 @@ app.get('/query', function (req, res) {
|
|||||||
return res.json(req.query);
|
return res.json(req.query);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/echo/multipartForm', upload.none(), function (req, res) {
|
|
||||||
return res.json(req.body);
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get('/redirect-to-ping', function (req, res) {
|
app.get('/redirect-to-ping', function (req, res) {
|
||||||
return res.redirect('/ping');
|
return res.redirect('/ping');
|
||||||
});
|
});
|
||||||
|
58
packages/bruno-tests/src/multipart/form-data-parser.js
Normal file
58
packages/bruno-tests/src/multipart/form-data-parser.js
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
/**
|
||||||
|
* Instead of using multer for example to parse the multipart form data, we build our own parser
|
||||||
|
* so that we can verify the content type are set correctly by bruno (for example application/json for json content)
|
||||||
|
*/
|
||||||
|
|
||||||
|
const extractParam = function (param, str, delimiter, quote, endDelimiter) {
|
||||||
|
let regex = new RegExp(`${param}${delimiter}\\s*${quote}(.*?)${quote}${endDelimiter}`);
|
||||||
|
const found = str.match(regex);
|
||||||
|
if (found != null && found.length > 1) {
|
||||||
|
return found[1];
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const init = function (app, express) {
|
||||||
|
app.use(express.raw({ type: 'multipart/form-data' }));
|
||||||
|
};
|
||||||
|
|
||||||
|
const parsePart = function (part) {
|
||||||
|
let result = {};
|
||||||
|
const name = extractParam('name', part, '=', '"', '');
|
||||||
|
if (name) {
|
||||||
|
result.name = name;
|
||||||
|
}
|
||||||
|
const filename = extractParam('filename', part, '=', '"', '');
|
||||||
|
if (filename) {
|
||||||
|
result.filename = filename;
|
||||||
|
}
|
||||||
|
const contentType = extractParam('Content-Type', part, ':', '', ';');
|
||||||
|
if (contentType) {
|
||||||
|
result.contentType = contentType;
|
||||||
|
}
|
||||||
|
if (!filename) {
|
||||||
|
result.value = part.substring(part.indexOf('value=') + 'value='.length);
|
||||||
|
}
|
||||||
|
if (contentType === 'application/json') {
|
||||||
|
result.value = JSON.parse(result.value);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
const parse = function (req) {
|
||||||
|
const BOUNDARY = 'boundary=';
|
||||||
|
const contentType = req.headers['content-type'];
|
||||||
|
const boundary = '--' + contentType.substring(contentType.indexOf(BOUNDARY) + BOUNDARY.length);
|
||||||
|
const rawBody = req.body.toString();
|
||||||
|
let parts = rawBody.split(boundary).filter((part) => part.length > 0);
|
||||||
|
parts = parts.map((part) => part.trim('\r\n'));
|
||||||
|
parts = parts.filter((part) => part != '--');
|
||||||
|
parts = parts.map((part) => part.replace('\r\n\r\n', ';value='));
|
||||||
|
parts = parts.map((part) => part.replace('\r\n', ';'));
|
||||||
|
parts = parts.map((part) => parsePart(part));
|
||||||
|
return parts;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.parse = parse;
|
||||||
|
module.exports.init = init;
|
10
packages/bruno-tests/src/multipart/index.js
Normal file
10
packages/bruno-tests/src/multipart/index.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const router = express.Router();
|
||||||
|
const formDataParser = require('./form-data-parser');
|
||||||
|
|
||||||
|
router.post('/mixed-content-types', (req, res) => {
|
||||||
|
const parts = formDataParser.parse(req);
|
||||||
|
return res.json(parts);
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
Loading…
Reference in New Issue
Block a user