weex请求库,可以像axios一样使用weex的stream.fetch
$ npm install weex-request --save
import weexRequest from "weex-request";
Performing a GET
request
import weexRequest from "weex-request";
// Make a request for a user with a given ID
weexRequest.get('/user?ID=12345')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
weexRequest(config)
// Send a POST request
weexRequest({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
});
// GET request for remote image
weexRequest({
method:'get',
url:'http://bit.ly/2mTM3nY',
type:'json', // 默认json 响应类型, json,text 或是 jsonp {在原生实现中其实与 json 相同)
headers:{
'Content-Type': 'application/json'
}
})
.then(function(response) {
response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
});
For convenience aliases have been provided for all supported request methods.
// 设置默认选项
weexRequest.default.baseURL = "https://api.github.com/";
weexRequest.default.headers = {
"Content-Type": "application/json"
};
You can intercept requests or responses before they are handled by then
or catch
.
import weexRequest from "weex-request";
// 设置默认选项
weexRequest.default.baseURL = "https://api.github.com/";
weexRequest.default.headers = {
"Content-Type": "application/json"
};
// body 格式化方式 json/query
// 默认设置为json { "name": "wq", "type": "json"}
// 设置为query 格式化形式为 name=wq&type=json
weexRequest.default.bodyParse = 'json';
// 拦截请求
weexRequest.interceptors.request = function(config) {
console.log("config", config);
return Promise.resolve(config);
};
// 拦截响应
weexRequest.interceptors.response = function(response) {
console.log("response", response);
return Promise.resolve(response);
};
export default weexRequest;