set content-type header only if not set by user

This commit is contained in:
Mirko Golze 2023-09-18 07:12:18 +02:00
parent 6f2bb55ecf
commit 7d4d1573af
2 changed files with 32 additions and 8 deletions

View File

@ -2,9 +2,13 @@ const { get, each, filter } = require('lodash');
const prepareRequest = (request) => { const prepareRequest = (request) => {
const headers = {}; const headers = {};
let contentTypeDefined = false;
each(request.headers, (h) => { each(request.headers, (h) => {
if (h.enabled) { if (h.enabled) {
headers[h.name] = h.value; headers[h.name] = h.value;
if (h.name.toLowerCase() === 'content-type') {
contentTypeDefined = true;
}
} }
}); });
@ -17,7 +21,9 @@ const prepareRequest = (request) => {
request.body = request.body || {}; request.body = request.body || {};
if (request.body.mode === 'json') { if (request.body.mode === 'json') {
axiosRequest.headers['content-type'] = 'application/json'; if (!contentTypeDefined) {
axiosRequest.headers['content-type'] = 'application/json';
}
try { try {
axiosRequest.data = JSON.parse(request.body.json); axiosRequest.data = JSON.parse(request.body.json);
} catch (ex) { } catch (ex) {
@ -26,12 +32,16 @@ const prepareRequest = (request) => {
} }
if (request.body.mode === 'text') { if (request.body.mode === 'text') {
axiosRequest.headers['content-type'] = 'text/plain'; if (!contentTypeDefined) {
axiosRequest.headers['content-type'] = 'text/plain';
}
axiosRequest.data = request.body.text; axiosRequest.data = request.body.text;
} }
if (request.body.mode === 'xml') { if (request.body.mode === 'xml') {
axiosRequest.headers['content-type'] = 'text/xml'; if (!contentTypeDefined) {
axiosRequest.headers['content-type'] = 'text/xml';
}
axiosRequest.data = request.body.xml; axiosRequest.data = request.body.xml;
} }
@ -56,7 +66,9 @@ const prepareRequest = (request) => {
query: get(request, 'body.graphql.query'), query: get(request, 'body.graphql.query'),
variables: JSON.parse(get(request, 'body.graphql.variables') || '{}') 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; axiosRequest.data = graphqlQuery;
} }

View File

@ -2,9 +2,13 @@ const { get, each, filter } = require('lodash');
const prepareRequest = (request) => { const prepareRequest = (request) => {
const headers = {}; const headers = {};
let contentTypeDefined = false;
each(request.headers, (h) => { each(request.headers, (h) => {
if (h.enabled) { if (h.enabled) {
headers[h.name] = h.value; headers[h.name] = h.value;
if (h.name.toLowerCase() === 'content-type') {
contentTypeDefined = true;
}
} }
}); });
@ -15,7 +19,9 @@ const prepareRequest = (request) => {
}; };
if (request.body.mode === 'json') { if (request.body.mode === 'json') {
axiosRequest.headers['content-type'] = 'application/json'; if (!contentTypeDefined) {
axiosRequest.headers['content-type'] = 'application/json';
}
try { try {
axiosRequest.data = JSON.parse(request.body.json); axiosRequest.data = JSON.parse(request.body.json);
} catch (ex) { } catch (ex) {
@ -24,12 +30,16 @@ const prepareRequest = (request) => {
} }
if (request.body.mode === 'text') { if (request.body.mode === 'text') {
axiosRequest.headers['content-type'] = 'text/plain'; if (!contentTypeDefined) {
axiosRequest.headers['content-type'] = 'text/plain';
}
axiosRequest.data = request.body.text; axiosRequest.data = request.body.text;
} }
if (request.body.mode === 'xml') { if (request.body.mode === 'xml') {
axiosRequest.headers['content-type'] = 'text/xml'; if (!contentTypeDefined) {
axiosRequest.headers['content-type'] = 'text/xml';
}
axiosRequest.data = request.body.xml; axiosRequest.data = request.body.xml;
} }
@ -54,7 +64,9 @@ const prepareRequest = (request) => {
query: get(request, 'body.graphql.query'), query: get(request, 'body.graphql.query'),
variables: JSON.parse(get(request, 'body.graphql.variables') || '{}') 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; axiosRequest.data = graphqlQuery;
} }