node 请求java_node请求java接口

node请求后台接口首先需要一个request模块

npm install request --save

var request = require("request")

Get方式请求:

var getUrl = 'https://xxxxxxxxxxxxxx?name=lele&age=11'

// 发送Get请求

request( url , function( error, response, data ) {

console.log( data )

})

url:请求地址,把需要的参数拼接在地址后面

error:错误信息

response:响应对象

data:返回数据

Post请求方式分三种

var postUrl = 'https://xxxxxxxxxxxxxxx/sendsms'

1.application/x-www-form-urlencoded: 普通http请求方式,与get类似,参数拼接到url后面

request.post({url:postUrl, form:{phone:'18801234567'}}, function(error, response, body) {

if (!error && response.statusCode == 200) {

//请求成功打印数据

console.log( body )

}

})

2.application/json: 参数为json格式的请求方式

var requestData = {

phone:"18801234567"

};

request({

url: postUrl,

method: "POST",

json: true,

headers: {

"content-type": "application/json",

},

body: JSON.stringify(requestData)

}, function(error, response, body) {

if (!error && response.statusCode == 200) {

console.log( body )

}

});

3.multipart/form-data : 文件上传

没真正试过。。。

2a19e995bb46?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值