Skip to content

Commit

Permalink
set content-type header only if not set by user
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirko Golze committed Sep 18, 2023
1 parent 6f2bb55 commit 7d4d157
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
20 changes: 16 additions & 4 deletions packages/bruno-cli/src/runner/prepare-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ const { get, each, filter } = require('lodash');

const prepareRequest = (request) => {
const headers = {};
let contentTypeDefined = false;
each(request.headers, (h) => {
if (h.enabled) {
headers[h.name] = h.value;
if (h.name.toLowerCase() === 'content-type') {
contentTypeDefined = true;
}
}
});

Expand All @@ -17,7 +21,9 @@ const prepareRequest = (request) => {
request.body = request.body || {};

if (request.body.mode === 'json') {
axiosRequest.headers['content-type'] = 'application/json';
if (!contentTypeDefined) {
axiosRequest.headers['content-type'] = 'application/json';
}
try {
axiosRequest.data = JSON.parse(request.body.json);
} catch (ex) {
Expand All @@ -26,12 +32,16 @@ const prepareRequest = (request) => {
}

if (request.body.mode === 'text') {
axiosRequest.headers['content-type'] = 'text/plain';
if (!contentTypeDefined) {
axiosRequest.headers['content-type'] = 'text/plain';
}
axiosRequest.data = request.body.text;
}

if (request.body.mode === 'xml') {
axiosRequest.headers['content-type'] = 'text/xml';
if (!contentTypeDefined) {
axiosRequest.headers['content-type'] = 'text/xml';
}
axiosRequest.data = request.body.xml;
}

Expand All @@ -56,7 +66,9 @@ const prepareRequest = (request) => {
query: get(request, 'body.graphql.query'),
variables: JSON.parse(get(request, 'body.graphql.variables') || '{}')
};
axiosRequest.headers['content-type'] = 'application/json';
if (!contentTypeDefined) {
axiosRequest.headers['content-type'] = 'application/json';
}
axiosRequest.data = graphqlQuery;
}

Expand Down
20 changes: 16 additions & 4 deletions packages/bruno-electron/src/ipc/network/prepare-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ const { get, each, filter } = require('lodash');

const prepareRequest = (request) => {
const headers = {};
let contentTypeDefined = false;
each(request.headers, (h) => {
if (h.enabled) {
headers[h.name] = h.value;
if (h.name.toLowerCase() === 'content-type') {
contentTypeDefined = true;
}
}
});

Expand All @@ -15,7 +19,9 @@ const prepareRequest = (request) => {
};

if (request.body.mode === 'json') {
axiosRequest.headers['content-type'] = 'application/json';
if (!contentTypeDefined) {
axiosRequest.headers['content-type'] = 'application/json';
}
try {
axiosRequest.data = JSON.parse(request.body.json);
} catch (ex) {
Expand All @@ -24,12 +30,16 @@ const prepareRequest = (request) => {
}

if (request.body.mode === 'text') {
axiosRequest.headers['content-type'] = 'text/plain';
if (!contentTypeDefined) {
axiosRequest.headers['content-type'] = 'text/plain';
}
axiosRequest.data = request.body.text;
}

if (request.body.mode === 'xml') {
axiosRequest.headers['content-type'] = 'text/xml';
if (!contentTypeDefined) {
axiosRequest.headers['content-type'] = 'text/xml';
}
axiosRequest.data = request.body.xml;
}

Expand All @@ -54,7 +64,9 @@ const prepareRequest = (request) => {
query: get(request, 'body.graphql.query'),
variables: JSON.parse(get(request, 'body.graphql.variables') || '{}')
};
axiosRequest.headers['content-type'] = 'application/json';
if (!contentTypeDefined) {
axiosRequest.headers['content-type'] = 'application/json';
}
axiosRequest.data = graphqlQuery;
}

Expand Down

0 comments on commit 7d4d157

Please sign in to comment.