tank-http-client.js基于 Needle.js的 Http 客户端

联合创作 · 2023-09-29 06:46

  tank-http-client.js 是一个基于 needle.js的 http 客户端。

  • 特性

  •  只支持node环境,浏览器不可运行
  •  支持链式调用
  •  单元测试已覆盖
  •  具有完善的文档
  • 安装

    npm install tank-http-client.js

    基本示例

    //导入
    const thc = require("tank-http-client.js")
    //设置基础URL地址 
    thc.setBaseUrl("http://localhost:3008")
    
    thc.get("/test")
        .query({id: 1})
        .send()
        .then((res) => {
            console.log(res)
        }).catch(err => {
        console.error(err)
    });
    //输出 ->{code: 200, method: 'GET', data: 'get_test', search: '1'}
    async () => {
        const res = await thc.get("/test")
            .query({id: 1})
            .send()
        console.log(res)
    }
    //输出 ->{code: 200, method: 'GET', data: 'get_test', search: '1'}

    更多示例

    delete

    //delete
    thc.delete("/test").query({id: 1}).send().then((res) => {
        //res: {code: 200, method: 'DELETE', data: 'delete_test', search: '1'}
    })

    post,put,patch

    //post support put patch 
    // thc.put("/test").xxx.send()
    // thc.patch("/test").xxx.send()
    thc.post("/test")
        .query({id: 1}).data({first: "1", nickname: "wind"}).data({
        name: "tank",
        nickname: "wind body"
    }).send().then((res) => {
        // res:{
        //     code: 200,
        //     method: 'POST',
        //     data: 'post_test',
        //     search: '1',
        //     params: {first: '1', nickname: 'wind body', name: 'tank'}
        // }
    })

    文件上传

    //upload
    thc.post("/upload").query({id: 1})
        .file({
            file1: path.join(__dirname, "tank.png"),
            file2: path.join(__dirname, "tank.png")
        })
        .send()
        .then((res) => {
    
        })
    
    //upload by buffer
    thc.post("/upload").query({id: 1})
        .bufferFile({
            file1: path.join(__dirname, "tank.png"),
            file2: path.join(__dirname, "tank.png")
        })
        .send()
        .then((res) => {
    
        })
浏览 6
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报