Unsupported media type when uploading form data to spring server

I am trying to upload a file to a remote spring server via an API, and I keep getting an unsupported media type error (415) even though I have already made the data into form data.

Here is the express http post request:

var FormData = require('form-data');
var fs = require('fs');
var form = new FormData();

form.append('pid', params.pid);
form.append('deliveryAttachment', fs.createReadStream(params.deliveryAttachment.path));

var url = someDomain + '/proj/new/deliveryAttachment';
requestLib({
    url: url,
    method: "POST",
    jar: getJar(),
    form: form
},function (error, response, body){     
    console.log(body)
});

And here is the Java Spring controller for reference:

    @RequestMapping(value = "proj/new/deliveryAttachment", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)
public String insertDeliveryAttachment(@RequestParam("pid") long pid,
        @RequestParam("deliveryAttachment") MultipartFile file) {
    try {
        DeliveryAttachment a = new DeliveryAttachment(file.getOriginalFilename(), pid);
        ps.insertDeliveryAttachment(a, file.getBytes());
        return String.valueOf(a.id);
    } catch (IOException e) {
        return "-1";
    }
}

This is the form data console log:

在这里输入图像描述

And the 415 response:

{
  "timestamp": 1494671395688,
  "status": 415,
  "error": "Unsupported Media Type",
  "exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
  "message": "Content type 'application/x-www-form-urlencoded' not supported",
  "path": "/proj/new/deliveryAttachment"
}

--UPDATE--

Alright, I just found out after reading request's docs that if you use form as the holder for the data, it will treat the data as application/x-www-form-urlencoded

eg; request.post({url:'http://service.com/upload', form: {key:'value'}}, function(err,httpResponse,body){ ... });

Meanwhile the correct key for multipart/form-data is formData

eg; request.post({url:'http://service.com/upload', formData: formData}, function optionalCallback(err, httpResponse, body) { ... });

I tried it, and now it's giving me a new error:

TypeError: Cannot read property 'name' of null at FormData._getContentDisposition


It seems that you are sending a POST request with Content-Type: 'application/x-www-form-urlencoded' , and your SpringController insertDeliveryAttachment() consumes multipart/form-data mime-type.

I would recommend you to change the consumes mime-type on your insertDeliveryAttachment() method to MediaType.APPLICATION_FORM_URLENCODED_VALUE


I've solved it. I did not use FormData and just went with inserting the values inside an object and it worked.

var data = {
    pid: params.pid,
    deliveryAttachment: fs.createReadStream(params.deliveryAttachment[0].path)
};  

var url = wfDomain + '/proj/new/deliveryAttachment';
requestLib({
    url: url,
    method: "POST",
    headers: {
        'Content-Type': 'multipart/form-data'
    },  
    jar: getJar(),
    formData: data
},function (error, response, body){ ... });
链接地址: http://www.djcxy.com/p/48730.html

上一篇: Ajaxupload和Spring MVC

下一篇: 将表单数据上传到弹簧服务器时不受支持的媒体类型