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
  • Can you add the server-side code that tries to parse the parameter? – Barmar Commented Nov 22, 2024 at 20:16
  • @Barmar, I can't. It's a third part API. If you want to see the docs, it's in Portuguese, see: docs.splitrisk.com.br/proposta/enviar-proposta. – Kaique Roque Commented Nov 24, 2024 at 22:28
  • Can you show the value of req.json() (redact personal information like account numbers). – Barmar Commented Nov 25, 2024 at 16:05
  • @Barmar, Yeah, that's an object like this: { "id_quotation_proposal": 1471, "id_vehicle": 2783, "id_address": 2754, "renavam": "123456789456", "billingType": "billet", "comission": 0.0, "value": 0.0, "creditCard": {}, "client": { "name": "John Due", "cpf": "123.456.789-00", "phone": "62999999999", "email": "[email protected]", "gender": 1, "birthdate": "1990-01-01" }, "mainDriver": { "name": "John Due", "cpf": "123.456.789-00", "birthdate": "1990-01-01" } } – Kaique Roque Commented Nov 25, 2024 at 19:38
  • That doesn't look anything like the Client or CreditCard objects shown at the link you gave. – Barmar Commented Nov 25, 2024 at 20:21
 |  Show 2 more comments

1 Answer 1

Reset to default 0

You 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