Skip to content

Commit 6af234f

Browse files
siadamacjohnny
authored andcommitted
[typescript-inversify] Fix multipart form uploads (#4131)
* Fix multipart form uploads * Update modules/openapi-generator/src/main/resources/typescript-inversify/HttpClient.mustache Co-Authored-By: Esteban Gehring <[email protected]> * re-generate samples
1 parent f901a84 commit 6af234f

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

modules/openapi-generator/src/main/resources/typescript-inversify/HttpClient.mustache

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ class HttpClient implements IHttpClient {
5151
};
5252

5353
private performNetworkCall(url: string, method: string, body?: any, headers?: Headers): Observable<HttpResponse> {
54+
55+
// when using fetch & a multipart upload, the requests content-type is handled by the browser, so should be left unset otherwise the multipart boundry is not added
56+
if(headers && headers["Content-Type"] === "multipart/form-data") {
57+
delete headers["Content-Type"];
58+
}
59+
5460
let promise = window.fetch(url, {
5561
method: method,
5662
body: body,

modules/openapi-generator/src/main/resources/typescript-inversify/api.service.mustache

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,12 @@ export class {{classname}} {
152152

153153
{{#hasFormParams}}
154154
let formData: FormData = new FormData();
155+
{{#isMultipart}}
156+
headers['Content-Type'] = 'multipart/form-data';
157+
{{/isMultipart}}
158+
{{^isMultipart}}
155159
headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
160+
{{/isMultipart}}
156161
{{#formParams}}
157162
{{#isListContainer}}
158163
if ({{paramName}}) {
@@ -174,7 +179,7 @@ export class {{classname}} {
174179
{{/formParams}}
175180

176181
{{/hasFormParams}}
177-
const response: Observable<HttpResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>> = this.httpClient.{{httpMethod}}(`${this.basePath}{{{path}}}{{#hasQueryParams}}?${queryParameters.join('&')}{{/hasQueryParams}}`{{#bodyParam}}, {{paramName}} {{/bodyParam}}{{#hasFormParams}}, body{{/hasFormParams}}, headers);
182+
const response: Observable<HttpResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>> = this.httpClient.{{httpMethod}}(`${this.basePath}{{{path}}}{{#hasQueryParams}}?${queryParameters.join('&')}{{/hasQueryParams}}`{{#bodyParam}}, {{paramName}} {{/bodyParam}}{{#hasFormParams}}, formData{{/hasFormParams}}, headers);
178183
if (observe == 'body') {
179184
return response.pipe(
180185
map(httpResponse => <{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>(httpResponse.response))

samples/client/petstore/typescript-inversify/HttpClient.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ class HttpClient implements IHttpClient {
4646
};
4747

4848
private performNetworkCall(url: string, method: string, body?: any, headers?: Headers): Observable<HttpResponse> {
49+
50+
// when using fetch & a multipart upload, the requests content-type is handled by the browser, so should be left unset otherwise the multipart boundry is not added
51+
if(headers && headers["Content-Type"] === "multipart/form-data") {
52+
delete headers["Content-Type"];
53+
}
54+
4955
let promise = window.fetch(url, {
5056
method: method,
5157
body: body,

samples/client/petstore/typescript-inversify/api/pet.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ export class PetService {
276276
formData.append('status', <any>status);
277277
}
278278

279-
const response: Observable<HttpResponse<any>> = this.httpClient.post(`${this.basePath}/pet/${encodeURIComponent(String(petId))}`, body, headers);
279+
const response: Observable<HttpResponse<any>> = this.httpClient.post(`${this.basePath}/pet/${encodeURIComponent(String(petId))}`, formData, headers);
280280
if (observe == 'body') {
281281
return response.pipe(
282282
map(httpResponse => <any>(httpResponse.response))
@@ -311,15 +311,15 @@ export class PetService {
311311
headers['Accept'] = 'application/json';
312312

313313
let formData: FormData = new FormData();
314-
headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
314+
headers['Content-Type'] = 'multipart/form-data';
315315
if (additionalMetadata !== undefined) {
316316
formData.append('additionalMetadata', <any>additionalMetadata);
317317
}
318318
if (file !== undefined) {
319319
formData.append('file', <any>file);
320320
}
321321

322-
const response: Observable<HttpResponse<ApiResponse>> = this.httpClient.post(`${this.basePath}/pet/${encodeURIComponent(String(petId))}/uploadImage`, body, headers);
322+
const response: Observable<HttpResponse<ApiResponse>> = this.httpClient.post(`${this.basePath}/pet/${encodeURIComponent(String(petId))}/uploadImage`, formData, headers);
323323
if (observe == 'body') {
324324
return response.pipe(
325325
map(httpResponse => <ApiResponse>(httpResponse.response))

0 commit comments

Comments
 (0)