axios 发送post请求

方案一

在node中使用axios以post的方式发送一张图片给某个server时:

let data = fs.createReadStream(__dirname + '/test.jpg')
		axios.post(url,{media:data,type:"image"})
		.then(function (response) {
		console.log(response.data);
		})
		.catch(function (error) {
		console.log(error);
		})

方案二

事实证明,这样做是完全没有用的,我尝试向另一个服务器poststream,返回的总是错误。然而,如果我使用request,下面这样的代码是完全没有问题的:

let data = fs.createReadStream(__dirname + '/test.jpg')
let form = {
type:"image",
media:data
}
request.post({url:url,formData:form},(err,res,body)=>{
if(err) console.log(err)
console.log(body)
})
// 注册事件方法
    register: function() {
      let registerUrl = this.GetServerUrl() + "/user/signup";
      let params = {
        username: this.username,
        password: this.password,
        email:    this.email,
        captcha:  this.captcha
      };
      this.axios
        .post(registerUrl, params)
        .then(response => {
          if(response.status == 0) {
            
          }
        });
    }
posted @ 2019-06-11 13:16  darylc  阅读(7522)  评论(0编辑  收藏  举报