I'm encountering an issue while trying to pass files with a JSON object. It seems like there might be an error in the controller where the 'consumes' and 'produces' declarations are possibly incorrect. I need assistance on how to attach one or more files to a document. The errors I encounter are usually of various types.
Console
{
"type" : "https://www.jhipster.tech/problem/problem-with-message",
"title" : "Unsupported Media Type",
"status" : 415,
"detail" : "Content type '' not supported",
"path" : "/api/documents",
"message" : "error.http.415"
}
API
- curl
curl -X POST --header 'Content-Type: multipart/form-data' --header 'Accept: application/problem+json' --header 'Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImF1dGgiOiJST0xFX0FETUlOLFJPTEVfVVNFUiIsImV4cCI6MTU4NTQ5ODc1NX0.4WT8jo-775CLWlCXe-gkyj0iARmP85w1OGoha-uc-yAVE2EFEPfsvTwE0LOn1Ypqh0-4Dh_FxiAmayIbbeyazw' {"type":"formData"} 'http://localhost:8080/api/documents'
- Response Body
{
"type": "https://www.jhipster.tech/problem/problem-with-message",
"title": "Unsupported Media Type",
"status": 415,
"detail": "Content type 'multipart/form-data;boundary=----WebKitFormBoundaryJHbbuWBb4WMI3DPz;charset=UTF-8' not supported",
"path": "/api/documents",
"message": "error.http.415"
}
DocumentResource.java
@PostMapping(value = "/documents", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Document> createDocument(@RequestBody Document document,
@ApiParam(value = "Content binary", required = true) @RequestPart(value = "file", required = true) MultipartFile file)
throws URISyntaxException, IllegalStateException, IOException {
log.debug("REST request to save Document : {}", document);
if (document.getId() != null) {
throw new BadRequestAlertException("A new document cannot already have an ID", ENTITY_NAME, "idexists");
}
if (!file.isEmpty()) {
String originalName = file.getOriginalFilename();
String filePath = destinationPath + originalName;
File destination = new File(filePath);
file.transferTo(destination);
} else {
throw new BadRequestAlertException("The file is null or empty", ENTITY_NAME, "isnotexists");
}
Document result = documentRepository.save(document);
return ResponseEntity
.created(new URI("/api/documents/" + result.getId())).headers(HeaderUtil
.createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.getId().toString()))
.body(result);
}
document.service.ts
createWithFiles(document: IDocument, file: File): Observable<EntityResponseType> {
const documentMultipartFormParam = 'document';
const fileMultipartFormParam = 'file';
const formData: FormData = new FormData();
const documentAsJsonBlob: Blob = new Blob([JSON.stringify(document)]);
formData.append(documentMultipartFormParam, documentAsJsonBlob);
formData.append(fileMultipartFormParam, file.name);
return this.http.post<IDocument>(this.resourceUrl, formData, { observe: 'response' });
}
Call to Service
document: IDocument;
file: File;
handleFileSelect($event) {
this.file = $event.target.files[0];
this.uploadFileToDeliverable();
}
uploadFileToDeliverable() {
this.subscribeToSaveResponse(this.documentService.createWithFiles(this.document, this.file))
}
document.model.ts
export interface IDocument {
id?: number;
name?: string;
extension?: string;
path?: string;
type?: string;
uuid?: string;
deliverables?: IDeliverable[];
}
export class Document implements IDocument {
constructor(
public id?: number,
public name?: string,
public extension?: string,
public path?: string,
public type?: string,
public uuid?: string
) { }
}