import { fromFetch } from 'rxjs/fetch'; import { switchMap } from 'rxjs/operators'; interface FetchRequest<TBody> { method?: FetchMethod; body?: TBody; headers?: FetchHeaders; isBlobRequest?: boolean; } export const request = <TBody extends BodyInit = BodyInit>( url: string, fetchRequest?: FetchRequest<TBody>, contentType: string ) => { const { method = 'POST', body, headers } = fetchRequest ?? {}; const { dispatch } = createStore(); let request = new Request(`${domainUrl}${url}`, { method, body: body ? body : null, headers: { ...headers, 'Content-type': contentType , }, credentials: 'include', }); return fromFetch(request).pipe( switchMap((response: Response) => { if (response.status === SUCCESS_CODE) { if (isBlobRequest) { return response.blob(); } return response.text(); } else if (response.status === USER_SESSION_EXPIRED_CODE) { dispatch(authAsyncActions.setSessionExpired()); throw response; } else { // This triggers error-handling and allows us to inspect the entire // error response including its status and body throw response; } }) ); }; const callUploadAPI = (file) => { let formData = new FormData(); formData.append('file', file); request(`urlUploadFile`, { method: 'POST', body: formData}, 'application/vnd.ms-excel') }
En el código anterior, estoy usando fromFetch de "rxjs/fetch" para llamar a la API POST REST y pasar el objeto "Solicitud" en fromFetch().
La interfaz de solicitud está dentro del texto mecanografiado según la siguiente captura de pantalla.

El backend es el servidor de matraz de Python y en el lado del backend en el código de Python que estoy usando
file = request.files['file']
para obtener el archivo que funciona cuando llamo a la API a través de Postman, pero cuando llamo a la API a través del código de interfaz no se obtiene el archivo.
¿Cómo puedo configurar el tipo "Cuerpo" de Solicitud como "datos de formulario" y el tipo "CLAVE" como Archivo en el código de interfaz?
Está configurando explícitamente el Content-type en application/vnd.ms-excel , pero no necesita configurar este encabezado
Hasta donde yo sé, si el body de una solicitud de recuperación es un objeto FormData , el navegador establece automáticamente el encabezado del tipo de contenido en multipart/form-data