The World’s Largest Online Community for Developers

'; java - Pass file and Json data in Post request - LavOzs.Com

I need pass files with json object but recive the same error. I think it is possible that in the controller, consumes and produces are not correctly defined. I need to know how to attach one or more files to a document. Errors are almost always of a different type.


  "type" : "",
  "title" : "Unsupported Media Type",
  "status" : 415,
  "detail" : "Content type '' not supported",
  "path" : "/api/documents",
  "message" : "error.http.415"


  • 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": "",
  "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"

@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);

        } else {
                throw new BadRequestAlertException("The file is null or empty", ENTITY_NAME, "isnotexists");

        Document result =;
        return ResponseEntity
                .created(new URI("/api/documents/" + result.getId())).headers(HeaderUtil
                        .createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.getId().toString()))


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);
    return<IDocument>(this.resourceUrl, formData, { observe: 'response' });

call to service

document: IDocument;
file: File;

handleFileSelect($event) {
    this.file = $[0];

uploadFileToDeliverable() {
    this.subscribeToSaveResponse(this.documentService.createWithFiles(this.document, this.file))


export interface IDocument {
  id?: number;
  name?: string;
  extension?: string;
  path?: string;
  type?: string;
  uuid?: string;
  deliverables?: IDeliverable[];

export class Document implements IDocument {
    public id?: number,
    public name?: string,
    public extension?: string,
    public path?: string,
    public type?: string,
    public uuid?: string
  ) { }

I had the same issue a while ago, till I did this:

const st = JSON.stringify(json);

const blob = new Blob([st], { type: 'application/json' });

const file = new File([ blob ], 'FileName.json');

const formData = new FormData();
formData.append('file', file, 'FileName.json');

My original answer:

Is Java “pass-by-reference” or “pass-by-value”?
HTTP GET with request body
How to use to fire and handle HTTP requests?
How to avoid Java code in JSP files?
HTTP response code for POST when resource already exists
Posting a File and Associated Data to a RESTful WebService preferably as JSON
How do I POST JSON data with cURL?
Vue.js and .NET WEBAPI2 POST from Axios 405 or 415 error
Process Json request with Multipart File