1 Star 0 Fork 0

inlym / jsreq

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

jsreq

一个简单易用的Node.js HTTP请求客户端,支持Promise。

安装

使用NPM安装

$ npm install jsreq

使用CNPM安装

$ cnpm install jsreq

使用

首先引用

const jsreq = require('jsreq');

支持多种调用方式,返回一个Promise。

let url = 'http://www.example.com'
let options = {
    baseURL:'http://www.example.com',
    url:'/abc',
    path:'/path/to?name=inlym'
    method:'POST',
    data:'I am data',
    headers:{
        aaa:'aaa',
        bbb:'bbb'
    },
    responseType:'text'
}
jsreq(url,options).then(console.log).catch(console.log)  // 方式一

jsreq(url).then(console.log).catch(console.log)  // 方式二

jsreq(options).then(console.log).catch(console.log)  // 方式三

请求参数

对于url,你可以单独拿出来放在参数的第1项,也可以放在 options 参数中。

其中 options 支持以下参数

let options = {
    baseURL:'http://www.example.com',
    url:'https://www.example.com',
    uri:'https://www.example.com',  // url的别名
    path:'/path/to?name=inlym',
    query:{                         // query对象
        age:19,
        isGood:true
    },
    method:'POST',
    data:'I am data',
    headers:{
        aaa:'aaa',
        bbb:'bbb'
    },
    responseType:'text'
}

响应

响应是一个对象,包含以下属性:

  1. statusCode
  2. statusMessage
  3. headers
  4. data

例如

{
    statusCode:200,
    statusMessage:'OK',
    headers:{
        'date': 'Mon, 30 Mar 2020 14:13:26 GMT',
        'content-type': 'application/json; charset=utf-8',
        'content-length': '1179',
    },
    data:'I am response data'
}
MIT License Copyright (c) 2020 inlym Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

An easy and simple Node.js HTTP client. 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/inlym/jsreq.git
git@gitee.com:inlym/jsreq.git
inlym
jsreq
jsreq
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891