[英]Multipart form parse error - Invalid boundary in multipart: None
I am very frustrated and could not find the soloution:我很沮丧,找不到解决方案:
I am creating a project using angularjs and nodejs.I get image data from angular side in node.js and send this data to further api.I got error我正在使用 angularjs 和 nodejs 创建一个项目。我从 node.js 的 angular 端获取图像数据,并将此数据发送到进一步的 api。我收到错误
{
"error": {
"detail": "Multipart form parse error - Invalid boundary in multipart: None"
}
}
here is my code in nodejs side:这是我在 nodejs 端的代码:
var request = require('request');
console.log(req.files);
var data = {
website:'www.gamail.xom',
contact_number:'dsdsd',
services_offered:'dsadasd',
contact_name:'dasdas',
provider_category:'exchange',
email:'kk@gmail.com',
image:req.files
}
var api_url = global.common.base_url + 'vcard/1.0.0/visit_card/' + req.param('uuid') +'/';
request({
url: api_url,
method: 'PUT',
headers: {
'Content-Type': 'multipart/form-data',
'Authorization': 'Bearer '+req.cookies.apitoken
},
json: data
}, function(error, response, body) {
if(response.statusCode == 200 && !error){
res.end(JSON.stringify(body));
}else{
res.send(response.statusCode, { error: body });
}
});
}
In req.files i get在 req.files 我得到
{ image:
[ { fieldName: 'image[0]',
originalFilename: 'icon_dd_chart_grey.png',
path: 'C:\\Users\\karakuma\\AppData\\Local\\Temp\\op74_gLSzPs-_49aT1GF0si
7.png',
headers: [Object],
size: 1474,
name: 'icon_dd_chart_grey.png',
type: 'image/png' } ] }
Try defining the content type as follows.尝试如下定义内容类型。
content_type='multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
I was facing the same issue and it worked for me in python.我遇到了同样的问题,它在 python 中对我有用。
I also faced this issue while trying to upload file.我在尝试上传文件时也遇到了这个问题。 For me the issue was the FormData, which was coming as Object
instead of FormData instance
对我来说,问题是 FormData,它作为Object
而不是FormData instance
So i converted my object to FormData using below code:所以我使用下面的代码将我的对象转换为 FormData:
const getFormData = object => Object.keys(object).reduce((formData, key) => { formData.append(key, object[key]); return formData; }, new FormData());
And the just executed my post request, in my case using Vue resource:刚刚执行了我的 post 请求,在我的情况下使用 Vue 资源:
return Vue.http.post(url, getFormData(formData), { headers: { 'Content-Type': 'multipart/form-data' } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
I have been facing this problem in angular 11 connected to Django rest API, I was able to curl with something like this in order to upload a JSON with a form:我在连接到 Django rest API 的 angular 11 中遇到了这个问题,我能够用这样的东西来卷曲,以便上传一个带有表单的 JSON:
curl -X POST -S \
-H 'Accept: application/json' \
-u "user:password" \
-F "name=name" \
-F "label=mylabel" \
-F "otherfields=something" \
-F "photo=@./example.png;type=image/png" \
http://127.0.0.1:8000/api/v1/item/
But I was getting that exception using my header as httpOptions:但是我使用我的标头作为 httpOptions 得到了这个异常:
'content-type': 'multipart/form-data',
So I just commented out the content-type
and it seems angular is so clever that he creates the header for you and will set the multipart together with the boundaries.所以我只是注释掉了content-type
,看起来 angular 非常聪明,他为你创建了标题,并将多部分与边界一起设置。
For more information on this: What is the boundary in multipart/form-data?有关更多信息: multipart/form-data 的边界是什么?
let data = fs.createReadStream('/home/insert/screen03.jpg');
let form = new FormData();
form.append('files', data, 'test.jpg');
form.getLength((err, length) => {
if (err) { reject(err); }
let headers = Object.assign({ 'Content-Length': length, 'Authorization': `Token b84db005a7fc1e5471a763aa42fcc5734b7bb22a` }, form.getHeaders());
return axios.post(`http://192.168.88.252:8000/resources/`, form, { headers: headers })
.then(res => console.log(res.data))
.catch(error => console.log(error.response.data))
});
There is no need to mention the content type in header, fetch() will detect it's content type by itself.不需要在 header 中提及内容类型, fetch() 会自己检测它的内容类型。
let formData = new FormData()
formData.append("email", email);
formData.append("password", password);
formData.append("image", image);
const response = await fetch('http://localhost:8000/api/authentication/register/', {
method: 'POST',
headers: {'X-CSRFToken': csrftoken}, //django specific
body: formData,
});
A boundary is just the 'key' to separate the multiple "parts" of a multipart payload.边界只是分隔多部分有效负载的多个“部分”的“关键”。 Normally something like '&' is enough to separate the variables but you need something more unique to separate the payloads within the payload comment通常像'&'这样的东西足以分隔变量,但你需要更独特的东西来分隔有效载荷注释中的有效载荷
You can use any value that not occurs in the data sent.您可以使用发送的数据中未出现的任何值。
NOTE: Because boundary delimiters must not appear in the body parts being encapsulated, a user agent must exercise care to choose a unique boundary parameter value.注意:因为边界分隔符不能出现在被封装的主体部分中,所以用户代理必须小心选择唯一的边界参数值。
The simplest boundary delimiter line possible is something like "---", with a closing boundary delimiter line of "-----".可能的最简单的边界分隔线类似于“---”,闭合边界分隔线为“-----”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.