awyhttp

small,simple http client

Usage no npm install needed!

<script type="module">
  import awyhttp from 'https://cdn.skypack.dev/awyhttp';
</script>

README

超级简单的HTTP客户端请求库

为了可以降低回调深度,并且能够更好的集成async和await。请求返回的结果是Promise。

注意headers选项使用小写,尤其是content-type等常用字段,因为在代码内部使用了小写的方式传递。

GET请求


const awyhttp = require('awyhttp');


awyhttp.get('http://localhost:2021/')
.then(data => {
    console.log(data);
}, err => {
    console.log(err);
}).catch(err => {
    console.log(err);
});




POST请求


awyhttp.post('http://localhost:2021/pt', {
    data : {
        name : 'Albert Einstein',
        identity : 'physics'
    }
})
.then(data => {
    console.log(data);
}, err => {
    console.log(err);
});


上传文件


awyhttp.upload('http://localhost:2021/upload2', {
    file : '/home/wy/tmp/fengye.jpg',
    upload_name : 'image'
})
.then(data => {
    console.log(data);
}, err => {
    console.log(err);
});


下载文件


var img_url = 'https://api.w3xm.top/media/images/u/u195f09b89a97fe441699debbe2b4600f21027072.png';

awyhttp.download(img_url, {
    method : 'GET',
    target : '/tmp/dtest.png'
}).then(data => {
    console.log(data);
}, err => {
    console.log(err);
}).catch(err => {
    console.log(err);
});

PUT请求


var put_data = "Unix's name is not Unix";

//要在async声明的函数中使用
var r = await awyhttp.put(url, {
    data : put_data
    headers : {
        'content-type' : 'text/plain'
    }
});

DELETE 请求

//注意要在async声明的函数中使用
var r = await awyhttp.delete(url);

文档