npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@esbiya/requests

v1.4.1

Published

a network requests lib

Readme

a network requests lib

Install

npm install -g @esbiya/requests

Author

👤 esbiya

Usage

const requests = require("@esbiya/requests");

(async function() {
    const resp = await requests.get(`https://www.baidu.com/`);
    console.log(resp.text);
})();

使用 headers

async function headerTest() {
    const resp = (await requests.get(`http://127.0.0.1:3000/api/v1/header-test`, {
        headers: {
            "hello": "world",
        }
    })).json();
    return resp["hello"] === "world"
}

使用代理

async function proxyTest() {
    const resp = (await requests.get("http://127.0.0.1:3000/api/v1/proxy-test", {
        proxy: "http://127.0.0.1:8888",
        verify: false
    })).text;
    return resp === "127.0.0.1"
}
支持 s5/http/https 代理:
s5: socks5://{username}:{password}@{ip}:{port}
http: http://{ip}:{port}
https: https://{ip}:{port}

使用 cookie

async function cookieTest() {
    const resp = (await requests.get("http://127.0.0.1:3000/api/v1/cookie-test", {
        // 方式 1
        cookies: {
            "hello": "world",
            "test1": "test2",
        },
        // 方式 2
        // cookies: "hello=world; test1=test2",
    })).text;
    return resp == "hello=world; test1=test2"
}

禁止重定向

async function redirectTest() {
    const resp = await requests.get("http://127.0.0.1:3000/api/v1/redirect-test", {
        params: {
            "redirectUrl": "http://test.demo.com/redirect",
        },
        followRedirect: false
    });
    return resp.statusCode === 302 && resp.location() === "http://test.demo.com/redirect"
}

get params 示例

async function paramsTest() {
    let params = {
        "hello": "world"
    }
    const resp = (await requests.get("http://127.0.0.1:3000/api/v1/params-test", {
        params: params
    })).json();
    return resp["hello"] === params["hello"];
}

post form 表单示例

async function formTest() {
    let form = {
        hello: 'world'
    }
    const resp = (await requests.post("http://127.0.0.1:3000/api/v1/form-test", {
        form: form  // 或者 hello=world
    })).json();
    return resp["hello"] === form["hello"];
}

post payload 示例

async function jsonTest() {
    let payload = {
        hello: 'world'
    }
    const resp = (await requests.post("http://127.0.0.1:3000/api/v1/json-test", {
        json: payload,
        headers: { 'Content-Type': 'application/json' },
    })).json();
    return resp["hello"] === payload["hello"];
}

post 二进制 示例

async function jsonTest() {
    let body = fs.readFileSync(`test.jpg`);
    const resp = await requests.post("http://127.0.0.1:3000/api/v1/binary-test", {
        body: body,
    });
    return resp.text === calculateFileHash("./test.jpg")
}

post 多表单示例

async function formDataTest() {
    const resp = await requests.post("http://127.0.0.1:3000/api/v1/formdata-test", {
        headers : { 'Content-Type' : 'multipart/form-data' },
        formData: {
            img: fs.createReadStream("./test.jpg")
        }
    });
    return resp.text === calculateFileHash("./test.jpg")
}

文件下载示例

async function downloadTest() {
    (await requests.get("http://127.0.0.1:3000/api/v1/download-test")).saveFile("test1.jpg");
    return calculateFileHash("test1.jpg") === calculateFileHash("test.jpg")
}

session 代理示例

async function sessionProxyTest() {
    const session = requests.session({
        proxy: "http://127.0.0.1:8888"
    });
    const resp = await session.get("http://127.0.0.1:3000/api/v1/proxy-test", { verify: false });
    return resp.text === "127.0.0.1"
}

session 设置 cookies 示例

// 用法 1
const session = requests.session();
session.updateCookies(`hello=world`, `http://www.baidu.com`);
session.updateCookies({
    "hello": "word"
}, `http://www.baidu.com`);
session.updateCookies([{
    key: "hello",
    value: "world",
    domain: "www.baidu.com"
}]);

// 用法 2
// const session = requests.session({
//     cookies: `xxx=yyy`
//     cookies: {
//         'xxx': 'yyy'
//     }
//     cookies: [{
//         key: 'xxx',
//         value: 'yyy'
//     }]
// });

const resp = await session.get("http://wwww.baidu.com/");
console.log(resp.text)

session 设置代理示例(使用代理默认忽略证书验证)

const session = requests.session({ proxy: `http://127.0.0.1:8888` })

session 设置 keepAalive

const session = requests.session({ keepAlive: true })

获取响应 html 解析对象(使用 cheerio, 具体用法请查看 cheerio 文档: https://github.com/cheeriojs/cheerio)

async function downloadTest() {
    const session = requests.session();
    const resp = await session.get("http://127.0.0.1:3000/api/v1/input-form-test");
    console.log(resp.document())
}

获取响应 html 中的 input form 表单

async function downloadTest() {
    const session = requests.session();
    const resp = await session.get("http://127.0.0.1:3000/api/v1/input-form-test");
    console.log(resp.inputForm('payForm'))
}

自动获取响应中的 <script>var data = JSON.parse('{\"error\":\"\"}'); </script> 并转化为标准 json 格式

async function downloadTest() {
    const session = requests.session();
    const resp = await session.get("http://127.0.0.1:3000/api/v1/parse-json-test");
    console.log(resp.parseJSON())
}

响应接口说明

| 接口 | 说明 |
|--- | --- | | resp.bytes | 响应字节流 | | resp.text | 响应文本 | | resp.json() | 获取标准 json 格式响应 | | resp.callbackJSON() | 自动处理 callback({"1": "2"}) 类型数据为标准 json 格式, 可指定 callback 名称, 如 resp.callbakcJSON('cb') 解析 ' cb({})' | | resp.cost() | 请求耗时: 毫秒(ms) | | resp.setEncoding('gbk') | 设置响应编码格式 | | resp.saveFile('test.jpg') | 响应存入本地文件 | | resp.location() | 获取重定向地址, 参数: load: boolean, true 表示获取 window.location.href=`` 类型的跳转 url, false 表示获取 302 跳转地址, 默认 false | | await resp.cookies() | 获取响应 cookie, touch.Cookie 数组 | | await resp.cookieString() | 获取响应 cookie 字符串, "111=222; 333=444" | | await resp.cookieMap() | 获取响应 cookie 标准 json 格式, {"111": "222"} | | await resp.cookieArrayMap() | 获取响应标准 cookie 格式数组, [{key: "111", value: "222", domain: "xxx"}] | | resp.content | 响应内容 | | resp.charset | 响应字符编码 | | resp.contentType | 响应类型 | | resp.contentLength | 响应长度 | | resp.headers | 响应头 | | resp.uri | 响应 url | | resp.httpVersion | 请求 http 版本 |

Show your support

Give a ⭐️ if this project helped you!


This README was generated with ❤️ by readme-md-generator