AngularJS + Spring: 415 unsupported media type

http post gets error while loading multi-page 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

@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 i am stuck to find a solution please help me find a solution

+1


source to share


2 answers


HTTP error 415 means that the content of the request does not match the correct format.

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



The solution is to specify your content type "application / json" in your post request, or remove the @RequestBody annotation.

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

+2


source


You have to send a value multipart/form-data

, not undefined

in a header Content-Type

(the header Accept

sent from the client should be application/json

). Also make sure the server side method uses this media type.



0


source







All Articles