axios post request to send form data

后端 未结 9 2410
走了就别回头了
走了就别回头了 2020-11-22 03:40

axios POST request is hitting the url on the controller but setting null values to my POJO class, when I go through developer tools in chrome, the payload conta

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 03:55

    In my case I had to add the boundary to the header like the following:

    const form = new FormData();
        formData.append(item.name, fs.createReadStream(pathToFile));
    
        const response = await axios({
            method: 'post',
            url: 'http://www.yourserver.com/upload',
            data: form,
            headers: {
            'content-type': `multipart/form-data; boundary=${form._boundary}`,
            },
        });
    

    This solution is also useful if you're working with React Native.

提交回复
热议问题