0

How to send json parameter and multipart/form-data file in one request with vue, just as postman does? postman request

I've searched how to submit "multipart/form-data" from VueJs . What I want is image file in multipart format, and other parameters in one json object(content-type is json). All the parameters are sent as a single http request.

The main difference is how to send json parameters and multipart file in a single http post request?

3
  • Possible duplicate of how to submit "multipart/form-data" from VueJs Commented Nov 7, 2019 at 15:18
  • @MartenCatcher I've searched that question. I want image file in multipart format, and other parameters in json. All the parameters are sent as a single http request. Commented Nov 7, 2019 at 15:23
  • @user7328234 you can't send multipart and json in a single request, that won't work. It's easier to add your parameters to the multipart request Commented Nov 7, 2019 at 15:46

2 Answers 2

1

First of all, this is not a vue related question. There is a solution in this answer though. Another solution (if content-type in request is not a priority) would be to use simple FormData. You can easily insert your image and send the json as a plaintext. This would however require additional parsing / mapping to your model at server side.

Sign up to request clarification or add additional context in comments.

Comments

0

i have also faced the same issue and i would like to share what i did. I am trying to upload image,kind, name and eventID.

let logo1 = new FormData
logo1.append('image', this.logo1)
logo1.append('kind', 'logo1')
logo1.append('name', this.$refs.logo1.files[0].name )
logo1.append('eventID',eventID)


axios.post(`/upload`, logo1,
            
   {
      headers:{
          'Authorization' : `${token}`,
          'Content-Type': `multipart/form-data; boundary=${logo1._boundary}` 
  },    

  }).then((res) => {

     console.log(res)


  }).catch((error) => {

    console.log(error)

  })

Note: The postdata param format should be (url , FormData) and not (url, {data: FormData})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.