Is there a example of multipart/form-data request?
Thanks for so awesome library! I'm using openapi-typescript-codegen so many projects.
I have in trouble. I wanna use in React Native project for image file upload usecase with expo-image-picker. But I couldn't implements this with FormData pattern and Blob pattern. Is there an example of multipart/form-data request?
Hi,
I had the same issue. Using this in a React Native project and POSTing with FormData.
There is a bug in the codegen tool.
I have managed to get this to work in the end.
- Issue with default export in
request.js
const getFormData = (options) => {
if (options.formData) {
//const formData = new form_data_1.default(); // <-- Had to change this
const formData = new form_data_1(); // <-- To this
return formData;
}
return undefined;
};
- Issue with
request.jsingetHeaders()
This test do not check options.formData if it is set so the request do not have any data.
...
if (options.body) {
if (options.mediaType) {
headers['Content-Type'] = options.mediaType;
}
else if (isBlob(options.body)) {
headers['Content-Type'] = options.body.type || 'application/octet-stream';
}
else if (isString(options.body)) {
headers['Content-Type'] = 'text/plain';
}
else if (!isFormData(options.body)) {
headers['Content-Type'] = 'application/json';
}
}
...
This is due to the generated code:
static postUpload(formData) {
return (0, request_1.request)(OpenAPI_1.OpenAPI, {
method: 'POST',
url: '/Upload',
formData: formData, // <-- Will not work due to the test above
mediaType: 'multipart/form-data'
});
A workaround here is to do this by setting the body:
static postUpload(formData) {
return (0, request_1.request)(OpenAPI_1.OpenAPI, {
method: 'POST',
url: '/Upload',
body: formData, // <-- Passing as body due to the test described above
mediaType: 'multipart/form-data'
});
PS: If you use the body insted of formData the issue in pt. 1 will also never happen.
I hope this help both you and the devs. :)
Yep, this happens to me too, hope a fix can be merged soon