Skip to content

Commit

Permalink
Fixed issue in http
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Jan 24, 2025
1 parent 5ee68ce commit ff76274
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/initializers/create-piral-instance/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const flags: Array<Flag> = [
data,
{ 'content-type': 'application/json' },
undefined,
false,
true,
);
return result.success;
Expand Down
49 changes: 35 additions & 14 deletions src/tooling/piral-cli/src/common/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,23 +220,46 @@ export interface PostFormResult {
response?: object;
}

function createPayload(
data: any,
scheme: PublishScheme,
key: string,
customHeaders: Record<string, string>,
isForm: boolean,
): [any, Record<string, string>] {
if (isForm) {
const form = createAxiosForm(data);
const extraHeaders = form.getHeaders();
const headers: Record<string, string> = {
...standardHeaders,
...customHeaders,
...getAuthorizationHeaders(scheme, key),
...extraHeaders,
};
return [form, headers];
} else {
const headers: Record<string, string> = {
...standardHeaders,
...customHeaders,
...getAuthorizationHeaders(scheme, key),
};
return [data, headers];
}
}

export async function postData(
target: string,
scheme: PublishScheme,
key: string,
data: any,
customHeaders: Record<string, string> = {},
httpsAgent?: Agent,
httpsAgent: Agent = undefined,
isForm = false,
interactive = false,
): Promise<PostFormResult> {
const headers: Record<string, string> = {
...standardHeaders,
...customHeaders,
...getAuthorizationHeaders(scheme, key),
};

try {
const res = await axios.post(target, data, {
const [body, headers] = createPayload(data, scheme, key, customHeaders, isForm);
const res = await axios.post(target, body, {
headers,
httpsAgent,
maxContentLength: Infinity,
Expand All @@ -253,7 +276,7 @@ export async function postData(
error,
interactive,
httpsAgent,
(mode, token) => postData(target, mode, token, data, customHeaders, httpsAgent, false),
(mode, token) => postData(target, mode, token, data, customHeaders, httpsAgent, isForm, false),
(status, statusText, response) => {
if (status === 500) {
log('failedHttpPost_0065', response);
Expand Down Expand Up @@ -281,12 +304,10 @@ export function postForm(
key: string,
formData: FormDataObj,
customHeaders: Record<string, string> = {},
httpsAgent?: Agent,
httpsAgent: Agent = undefined,
interactive = false,
): Promise<PostFormResult> {
const form = createAxiosForm(formData);
const headers = form.getHeaders();
return postData(target, scheme, key, form, { ...customHeaders, ...headers }, httpsAgent, interactive);
return postData(target, scheme, key, formData, customHeaders, httpsAgent, true, interactive);
}

export function postFile(
Expand All @@ -296,7 +317,7 @@ export function postFile(
file: Buffer,
customFields: Record<string, string> = {},
customHeaders: Record<string, string> = {},
agent?: Agent,
agent: Agent = undefined,
interactive = false,
): Promise<PostFormResult> {
const data: FormDataObj = { ...customFields, file: [file, 'pilet.tgz'] };
Expand Down

0 comments on commit ff76274

Please sign in to comment.