Merge pull request #938 from adarshlilha/bugfix/code-generator-headers

fix(#901): fix headers in code generator
This commit is contained in:
Anoop M D 2023-11-18 09:32:19 +05:30 committed by GitHub
commit 42f6353ec8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 18 deletions

View File

@ -9,10 +9,11 @@ const CodeView = ({ language, item }) => {
const { storedTheme } = useTheme(); const { storedTheme } = useTheme();
const preferences = useSelector((state) => state.app.preferences); const preferences = useSelector((state) => state.app.preferences);
const { target, client, language: lang } = language; const { target, client, language: lang } = language;
const headers = item.draft ? get(item, 'draft.request.headers') : get(item, 'request.headers');
let snippet = ''; let snippet = '';
try { try {
snippet = new HTTPSnippet(buildHarRequest(item.request)).convert(target, client); snippet = new HTTPSnippet(buildHarRequest({ request: item.request, headers })).convert(target, client);
} catch (e) { } catch (e) {
console.error(e); console.error(e);
snippet = 'Error generating code snippet'; snippet = 'Error generating code snippet';

View File

@ -9,25 +9,17 @@ const createContentType = (mode) => {
case 'multipartForm': case 'multipartForm':
return 'multipart/form-data'; return 'multipart/form-data';
default: default:
return 'application/json'; return '';
} }
}; };
const createHeaders = (headers, mode) => { const createHeaders = (headers) => {
const contentType = createContentType(mode); return headers
const headersArray = headers
.filter((header) => header.enabled) .filter((header) => header.enabled)
.map((header) => { .map((header) => ({
return { name: header.name,
name: header.name, value: header.value
value: header.value }));
};
});
const headerNames = headersArray.map((header) => header.name);
if (!headerNames.includes('Content-Type')) {
return [...headersArray, { name: 'Content-Type', value: contentType }];
}
return headersArray;
}; };
const createQuery = (queryParams = []) => { const createQuery = (queryParams = []) => {
@ -56,13 +48,13 @@ const createPostData = (body) => {
} }
}; };
export const buildHarRequest = (request) => { export const buildHarRequest = ({ request, headers }) => {
return { return {
method: request.method, method: request.method,
url: request.url, url: request.url,
httpVersion: 'HTTP/1.1', httpVersion: 'HTTP/1.1',
cookies: [], cookies: [],
headers: createHeaders(request.headers, request.body.mode), headers: createHeaders(headers),
queryString: createQuery(request.params), queryString: createQuery(request.params),
postData: createPostData(request.body), postData: createPostData(request.body),
headersSize: 0, headersSize: 0,