Add method to upload files to api client

This commit is contained in:
Rene Pfeuffer
2019-08-29 16:37:57 +02:00
parent 51d423f4db
commit 01ecd144e1

View File

@@ -54,6 +54,19 @@ class ApiClient {
return this.httpRequestWithJSONBody("POST", url, contentType, payload); return this.httpRequestWithJSONBody("POST", url, contentType, payload);
} }
postBinary(contentType: string, url: string, files: File[]) {
let formData = new FormData();
for (let i = 0; i < files.length; i++) {
formData.append("file" + i, files[i]);
}
let options: RequestOptions = {
method: "POST",
body: formData
};
return this.httpRequestWithBinaryBody(options, contentType, url);
}
put(url: string, payload: any, contentType: string = "application/json") { put(url: string, payload: any, contentType: string = "application/json") {
return this.httpRequestWithJSONBody("PUT", url, contentType, payload); return this.httpRequestWithJSONBody("PUT", url, contentType, payload);
} }
@@ -84,6 +97,10 @@ class ApiClient {
method: method, method: method,
body: JSON.stringify(payload) body: JSON.stringify(payload)
}; };
return this.httpRequestWithBinaryBody(options, contentType, url);
}
httpRequestWithBinaryBody(options: RequestOptions, contentType: string, url: string) {
options = Object.assign(options, fetchOptions); options = Object.assign(options, fetchOptions);
// $FlowFixMe // $FlowFixMe
options.headers["Content-Type"] = contentType; options.headers["Content-Type"] = contentType;