-
+
+
+
} placement="bottom-end">
+
{
+ dropdownTippyRef.current.hide();
+ curlRequestTypeChange('http-request');
+ }}
+ >
+ HTTP
+
+
{
+ dropdownTippyRef.current.hide();
+ curlRequestTypeChange('graphql-request');
+ }}
+ >
+ GraphQL
+
+
+
{formik.touched.curlCommand && formik.errors.curlCommand ? (
{formik.errors.curlCommand}
diff --git a/packages/bruno-app/src/utils/codegenerator/har.js b/packages/bruno-app/src/utils/codegenerator/har.js
index 9bbd0eea9..479fcd67a 100644
--- a/packages/bruno-app/src/utils/codegenerator/har.js
+++ b/packages/bruno-app/src/utils/codegenerator/har.js
@@ -19,16 +19,23 @@ const createContentType = (mode) => {
}
};
+/**
+ * Creates a list of enabled headers for the request, ensuring no duplicate content-type headers.
+ *
+ * @param {Object} request - The request object.
+ * @param {Object[]} headers - The array of header objects, each containing name, value, and enabled properties.
+ * @returns {Object[]} - An array of enabled headers with normalized names and values.
+ */
const createHeaders = (request, headers) => {
const enabledHeaders = headers
.filter((header) => header.enabled)
.map((header) => ({
- name: header.name,
+ name: header.name.toLowerCase(),
value: header.value
}));
const contentType = createContentType(request.body?.mode);
- if (contentType !== '') {
+ if (contentType !== '' && !enabledHeaders.some((header) => header.name === 'content-type')) {
enabledHeaders.push({ name: 'content-type', value: contentType });
}
diff --git a/packages/bruno-app/src/utils/curl/curl-to-json.js b/packages/bruno-app/src/utils/curl/curl-to-json.js
index e76f4014a..44d9c4b2b 100644
--- a/packages/bruno-app/src/utils/curl/curl-to-json.js
+++ b/packages/bruno-app/src/utils/curl/curl-to-json.js
@@ -36,6 +36,12 @@ function getQueries(request) {
return queries;
}
+/**
+ * Converts request data to a string based on its content type.
+ *
+ * @param {Object} request - The request object containing data and headers.
+ * @returns {Object} An object containing the data string.
+ */
function getDataString(request) {
if (typeof request.data === 'number') {
request.data = request.data.toString();
@@ -44,7 +50,13 @@ function getDataString(request) {
const contentType = getContentType(request.headers);
if (contentType && contentType.includes('application/json')) {
- return { data: request.data.toString() };
+ try {
+ const parsedData = JSON.parse(request.data);
+ return { data: JSON.stringify(parsedData) };
+ } catch (error) {
+ console.error('Failed to parse JSON data:', error);
+ return { data: request.data.toString() };
+ }
}
const parsedQueryString = querystring.parse(request.data, { sort: false });
diff --git a/packages/bruno-app/src/utils/curl/index.js b/packages/bruno-app/src/utils/curl/index.js
index e16dc68a5..e478a8e7e 100644
--- a/packages/bruno-app/src/utils/curl/index.js
+++ b/packages/bruno-app/src/utils/curl/index.js
@@ -2,7 +2,7 @@ import { forOwn } from 'lodash';
import { convertToCodeMirrorJson } from 'utils/common';
import curlToJson from './curl-to-json';
-export const getRequestFromCurlCommand = (curlCommand) => {
+export const getRequestFromCurlCommand = (curlCommand, requestType = 'http-request') => {
const parseFormData = (parsedBody) => {
const formData = [];
forOwn(parsedBody, (value, key) => {
@@ -12,6 +12,22 @@ export const getRequestFromCurlCommand = (curlCommand) => {
return formData;
};
+ const parseGraphQL = (text) => {
+ try {
+ const graphql = JSON.parse(text);
+
+ return {
+ query: graphql.query,
+ variables: JSON.stringify(graphql.variables, null, 2)
+ };
+ } catch (e) {
+ return {
+ query: '',
+ variables: ''
+ };
+ }
+ };
+
try {
if (!curlCommand || typeof curlCommand !== 'string' || curlCommand.length === 0) {
return null;
@@ -24,6 +40,8 @@ export const getRequestFromCurlCommand = (curlCommand) => {
Object.keys(parsedHeaders).map((key) => ({ name: key, value: parsedHeaders[key], enabled: true }));
const contentType = headers?.find((h) => h.name.toLowerCase() === 'content-type')?.value;
+ const parsedBody = request.data;
+
const body = {
mode: 'none',
json: null,
@@ -31,11 +49,15 @@ export const getRequestFromCurlCommand = (curlCommand) => {
xml: null,
sparql: null,
multipartForm: null,
- formUrlEncoded: null
+ formUrlEncoded: null,
+ graphql: null
};
- const parsedBody = request.data;
+
if (parsedBody && contentType && typeof contentType === 'string') {
- if (contentType.includes('application/json')) {
+ if (requestType === 'graphql-request' && (contentType.includes('application/json') || contentType.includes('application/graphql'))) {
+ body.mode = 'graphql';
+ body.graphql = parseGraphQL(parsedBody);
+ } else if (contentType.includes('application/json')) {
body.mode = 'json';
body.json = convertToCodeMirrorJson(parsedBody);
} else if (contentType.includes('text/xml')) {