当前位置:   article > 正文

node 通过axios发送post请求(FormData)_nodejs使用axios用post请求

nodejs使用axios用post请求

方案一

  1. const axios = require('axios')
  2. const FormData = require('form-data')
  3. const fs = require('fs')
  4. const sdUpscaleOnAzure = async (req, res) => {
  5. const data = new FormData()
  6. data.append('image', fs.readFileSync('/temp/ai/sd/download/1.png'))
  7. let config = {
  8. headers: {
  9. ...data.getHeaders(),
  10. Accept: 'application/json',
  11. },
  12. method: 'post',
  13. }
  14. config.url = 'https://www.baidu.com'
  15. config.data = data
  16. console.log(config)
  17. axios({
  18. ...config,
  19. })
  20. .then(async (response) => {
  21. res.send({
  22. code: 200,
  23. data: {
  24. response: response.data,
  25. },
  26. message: '成功',
  27. })
  28. })
  29. .catch((err) => {
  30. res.send({
  31. code: 400,
  32. data: {
  33. err,
  34. },
  35. message: '失败2',
  36. })
  37. })
  38. }

方案二

  1. const data = new FormData()
  2. let tempData = config.data
  3. for (let key in tempData) {
  4. data.append(key, tempData[key])
  5. }
  6. config.data = data
  7. config.headers['Content-Type'] =
  8. 'multipart/form-data; boundary=' + data.getBoundary()
  9. axios({
  10. ...config,
  11. })
  12. .then((response) => {
  13. res.send({
  14. code: 200,
  15. data: {
  16. response: response.data,
  17. },
  18. message: '成功',
  19. })
  20. })
  21. .catch((err) => {
  22. res.send({
  23. code: 400,
  24. data: {
  25. err,
  26. },
  27. message: '失败',
  28. })
  29. })

参考链接

https://chat.xutongbao.top/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/283503
推荐阅读
相关标签
  

闽ICP备14008679号