Satish
Satish

Reputation: 537

AngularJS + Spring: 415 unsupported media type

http post is getting error while uploading multipart data

var formData = new FormData();

formData.append("startDate",$("#startDate").val());
formData.append("File1",$("input[name='file']")[0].files[0]);
formData.append("File2",$("input[name='file2']")[0].files[0]);

$http.post("sampleurl",formData,
{ headers : 'Content-Type' : undefined},
transformRequest : angular.identity
}).then(function(data){
  alert(data);
    });
}

my server side code is

@RequestMapping(value = "sampleurl", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON)
    public @ResponseBody
    Response createSomething(
            @RequestBody Request request,
            HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse) {
            // code here
    }

what went wrong here, i am stucked to find the solution, please help me to find the solution

Upvotes: 1

Views: 15014

Answers (3)

Vallerious
Vallerious

Reputation: 640

Just a reminder to everyone that might wonder about the solution to this. Make sure that you are using multipart/form-data as content-type when sending FormData objects...I was using application/zip which was the particular type of file that I was uploading which is not correct and not needed, I suppose because the info about the type of the file is transferred too.

Upvotes: 0

Michal Gajdos
Michal Gajdos

Reputation: 10379

You should be sending multipart/form-data and not undefined value in your Content-Type header (Accept header sent from client should be application/json). Also make sure that the method on the server side consumes this particular media type.

Upvotes: 0

Mickael
Mickael

Reputation: 5736

An http error 415 means that content of request is not in the appropriate format.

Spring MVC '@RequestBody' expect a json body (with a Content-Type equal to 'application/json') and you explicitly set your Content-Type to undefined.

The solution is to set your content-type to 'application/json' in your post request or to remove @RequestBody annotation.

It seems that you try to upload files, the easier would be to remove @RequestBody annotation.

Upvotes: 2

Related Questions