这里有两种方法
第一种
使用request
const request = require('request')
const httpRequest = async (address) => {
let url = `http://localhost:2222/wx/api/ship/getShipFee`
var options = {
method: 'post',
url: url,
json: {
'shop_no': '11047059',
'origin_id': 'o_111',
'city': '深圳',
'cargo_price': '10.00',
'is_prepay': '1',
'receiver_name': '张三',
'receiver_address': '深圳市南山区南山大道时代骄子大厦',
'receiver_phone': '13760264461',
'source_id': '73753',
'ship_type_id': 3
},
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json' // 需指定这个参数 否则 在特定的环境下 会引起406错误
}
}
//
request(options, function (err, res, body) {
if (err) {
console.log(err)
} else {
console.log(body)
}
})
}
第二种 使用封装好的koa-request库
const koa2Req = require('koa2-request')
// const fxp = require('fast-xml-parser');
const httpRequest = async (address) => {
// const addr = encodeURI(address)
let url = `http://localhost:2222/wx/api/ship/getShipFee`
var options = {
method: 'post',
url: url,
json: {
'shop_no': '11047059',
'origin_id': 'o_111',
'city': '深圳',
'cargo_price': '10.00',
'is_prepay': '1',
'receiver_name': '张三',
'receiver_address': '深圳市南山区南山大道时代骄子大厦',
'receiver_phone': '13760264461',
'source_id': '73753',
'ship_type_id': 3
},
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
}
const res = await koa2Req(options)
return res.body.data
}
module.exports = {
httpRequest
}