[Node.js] 使用 axios post form-data

使用 axios 套件 post form-data 範例函式:

const axios = require('axios').create({  
  withCredentials: true
})

function postFormData (uri, data) {  
  const formData = new FormData()
  Object.keys(data).forEach(key => {
    formData.append(key, data[key])
  })

  const request = axios.post(uri, formData)
    .then(r => r.data)
    .catch(function (e) {
      console.error(e)
    })

  return request
}

postFormData ('/1/post/', { name: 'TEST' })  
  .then(r => {
    // ....
  })
ALL RIGHTS RESERVED. COPYRIGHT © 2018. Designed and Coded by Makee.io