admin管理员组文章数量:1122846
I'm trying to make a request using Axios and I need to send body as a FormData but I'm getting this issue:
Axios Error:
{
"errors": {
"expose": true,
"statusCode": 400,
"status": 400,
"body": "----------------------------524246740682154737031830\r\nContent-Disposition: form-data; name=\"proposalData\"\r\n\r\n{// my data is here}\r\n----------------------------524246740682154737031830--\r\n",
"type": "entity.parse.failed"
}
}
My code
In the body of request I have to send something like this
{
proposalData: {
id_quotation_proposal: Int;
id_vehicle: Int;
id_address: Int;
renavam: string;
billingType: string;
comission: float;
value: float;
client: {
name: string;
cpf: string;
phone: string;
email: string;
gender: Int;
birthdate: string;
};
creditCard: {}; // not required
};
vehicleInvoice: Buffer;// not required
vehicleDocument: Buffer;// not required
}
The docs says I have to send the body as a FormData and the proposalData as a serialized object.
Preparing data:
const body = await req.json();
const formData = new FormData();
formData.append('proposalData', JSON.stringify(body.proposalData));
Axios request:
const response = await axios.post(url, formData, {
headers: { Authorization: token, },
});
I tried this:
const response = await axios.post( url, formData, {
headers: {
Authorization: token,
...formData.getHeaders()
},
});
and this
headers: {
Authorization: token,
"Content-Type": "multipart/form-data"
},
PS.: When I use insomnia to request, it works.
Update: I made the request using the deprecated "request-promise" and it works. Look the code
import request from 'request-promise'
// rest of my code
try{
const response = await request({
method: 'POST',
uri: url,
headers: {
Authorization: token,
...formData.getHeaders(),
},
body: formData,
resolveWithFullResponse: true,
});
// rest of code
I'm trying to make a request using Axios and I need to send body as a FormData but I'm getting this issue:
Axios Error:
{
"errors": {
"expose": true,
"statusCode": 400,
"status": 400,
"body": "----------------------------524246740682154737031830\r\nContent-Disposition: form-data; name=\"proposalData\"\r\n\r\n{// my data is here}\r\n----------------------------524246740682154737031830--\r\n",
"type": "entity.parse.failed"
}
}
My code
In the body of request I have to send something like this
{
proposalData: {
id_quotation_proposal: Int;
id_vehicle: Int;
id_address: Int;
renavam: string;
billingType: string;
comission: float;
value: float;
client: {
name: string;
cpf: string;
phone: string;
email: string;
gender: Int;
birthdate: string;
};
creditCard: {}; // not required
};
vehicleInvoice: Buffer;// not required
vehicleDocument: Buffer;// not required
}
The docs says I have to send the body as a FormData and the proposalData as a serialized object.
Preparing data:
const body = await req.json();
const formData = new FormData();
formData.append('proposalData', JSON.stringify(body.proposalData));
Axios request:
const response = await axios.post(url, formData, {
headers: { Authorization: token, },
});
I tried this:
const response = await axios.post( url, formData, {
headers: {
Authorization: token,
...formData.getHeaders()
},
});
and this
headers: {
Authorization: token,
"Content-Type": "multipart/form-data"
},
PS.: When I use insomnia to request, it works.
Update: I made the request using the deprecated "request-promise" and it works. Look the code
import request from 'request-promise'
// rest of my code
try{
const response = await request({
method: 'POST',
uri: url,
headers: {
Authorization: token,
...formData.getHeaders(),
},
body: formData,
resolveWithFullResponse: true,
});
// rest of code
Share
Improve this question
edited Nov 26, 2024 at 1:23
Kaique Roque
asked Nov 22, 2024 at 19:58
Kaique RoqueKaique Roque
12 bronze badges
7
|
Show 2 more comments
1 Answer
Reset to default 0You should only send the information from body.client
, not the entire body
. And there's no mention of an option1
field in the API documentation, so I think each property should be a separate field in the FormData
.
const body = await req.json();
const formData = new FormData();
Object.entries(body.client).forEach(
([key, value]) => formData.append(key, value));
const response = await axios.post(url, formData, {
headers: {
Authorization: token,
},
});
本文标签: javascriptAxios Error quotentityparsefailedquot (When sending FormData)Stack Overflow
版权声明:本文标题:javascript - Axios Error "entity.parse.failed" (When sending FormData) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736301117a1931067.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
req.json()
(redact personal information like account numbers). – Barmar Commented Nov 25, 2024 at 16:05