D
D
Darkness2019-12-16 08:57:49
JSON
Darkness, 2019-12-16 08:57:49

How to send file and JSON data?

There is a task to execute sending of a file and the data.
When sending a file, you must attach a json object.

We create a `FormObj()` and put a file and a JSON object into it.

Technologies used:
- axios

Problem :
Attached object comes to backend as a string: `` I

specify headers when submitting:
`Content-Type': 'multipart/form-data`
`Accept: apllication/json;charset= UTF-8`

Code:

export const importLeads = function(form, delimiter, first_line_poss, selectedColumn, extraField){
  let data = {
    "options": {
      "delimeters": delimiter,
      "first_line_poss": first_line_poss
    },
    "colums": selectedColumn,
    "extraComuln": extraField || undefined
  }
  form.append("data", data)
  return api.put(`leads/import/csv`, form,  
  {
    headers: 
    {
      'Content-Type': 'multipart/form-data'
    }
  })


Request body:

-----------------------------9246871473245247581210159984
Content-Disposition: form-data; name="file"; filename="somefile.csv"
Content-Type: text/csv

$email,$name,$phone,Property name 1,Property name 2
[email protected],Sasha,89991234567,Property value,Property value
----- -----------------------9246871473245247581210159984
Content-Disposition: form-data; name="data"

[object Object]
-----------------------------9246871473245247581210159984--

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Planet_93, 2019-12-16
@AntonBrock

Try writing parameters using JSON.stringify().
Or add each parameter separately to formData.

form.append("file", file);
form.append("delimeters", delimiter);
form.append("first_line_poss", first_line_poss);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question