仅适用于调用showapi.com的接口! ES6 await async语法的Promise版本,请求库使用axios axios 文档地址
yarn add showapirequest_nodejs
npm i showapirequest_nodejs
const fs = require('fs')
const ShowapiRequest = require('showapirequest_nodejs')
//创建请求对象
const sdk = new ShowapiRequest(
'https://route.showapi.com/887-2',//接口地址
'appId',//替换为真实的appId
'sign'//替换为真实的密钥
)
sdk
.addFilePara('img', './static/xxx.png')//添加文件参数,
.post()//post方式发送请求,返回axios.post()的Promise
.then((res) => {
console.info("#####result:",res.data)
console.info("#######axios all response:",res)
})
.catch((error) => {
console.error(error)
})
const fs = require('fs')
const ShowapiRequest = require('showapirequest_nodejs')
//创建请求对象
const sdk = new ShowapiRequest(
'https://route.showapi.com/887-1',//接口地址
'appId',//替换为真实的appId
'sign'//替换为真实的密钥
)
sdk
.addTextPara('content','https://baidu.com')//添加普通参数
.get()//get方式发送请求,返回axios.get()的Promise
.then((res) => {
console.info("#####result:",res.data)
console.info("#######axios all response:",res)
})
.catch((error) => {
console.error(error)
})
const fs = require('fs')
const ShowapiRequest = require('showapirequest_nodejs')
//创建请求对象
const sdk = new ShowapiRequest(
'https://route.showapi.com/887-4',//接口地址
'appId',//替换为真实的appId
'sign'//替换为真实的密钥
)
sdk
.addBase64Para('imgData','./static/xxx.png')//添加base64参数
.post()//post方式发送请求,返回axios.post()的Promise
.then((res) => {
console.info("#####result:",res.data)
console.info("#######axios all response:",res)
})
.catch((error) => {
console.error(error)
})
本sdk使用了 ES6 Promises以及es6 async await特性, 请确保你的环境支持它
Kind: global class
ShowapiSDK构造方法
Param | Type | Default | Description |
---|---|---|---|
url | string |
接口调用地址 | |
appId | int |
应用id | |
sign | string |
应用sign | |
timeout | int |
30000 |
超时毫秒,默认为30秒 |
设置超时,针对ShowapiSDK对象生命周期有效
Kind: instance method of ShowapiSDK
Param | Type | Description |
---|---|---|
timeout | int |
超时毫秒 |
添加一个普通参数,每次post或者get后已添加参数会清空
Kind: instance method of ShowapiSDK
Returns: this
Param | Type |
---|---|
name | String |
value | String |
添加一个文件参数,每次post后已添加参数会清空 注意在浏览器端使用时只能使用上传input获取到文件后传入,不能使用文件路径作为参数。浏览器下无法使用fs包
Kind: instance method of ShowapiSDK
Returns: this
Param | Type | Description |
---|---|---|
name | String |
|
fileOrPath | String |
文件绝对路径或者文件对象(浏览器端只能使用文件对象) |
添加一个base64参数,每次post或者get后已添加参数会清空 注意在浏览器端使用时只能使用上传input获取到文件后传入,不能使用文件路径作为参数。浏览器下无法使用fs包
Kind: instance method of ShowapiSDK
Returns: this
Param | Type | Description |
---|---|---|
name | String |
|
fileOrPath | String |
需要转为base64字符串的文件路径或者原始文件对象(浏览器端只能使用文件对象) |
以post方式发送请求,使用axios包
Kind: instance method of ShowapiSDK
Returns: Promise
- axios.post()
以get方式发送请求,使用axios包
Kind: instance method of ShowapiSDK
Returns: Promise
- axios.get()