如何用最纯净的模块参数查询(node.js的) [英] How to query with parameters using Purest module (node.js)

查看:160
本文介绍了如何用最纯净的模块参数查询(node.js的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想传递一些参数与我的API请求,所以我可以得到的已经过滤的数据。

我使用了一个名为最纯净模块,它基本上是一个REST API客户端库。
它支持使得前pressive查询电话按最纯净的查询API

和,我的API提供商掌上及其有关文档这里说以下更多信息

的contentType

 文章=只返回文章
视频=只返回视频或文章与嵌入视频
图像=只返回图像

排序

 以最新=回报项目的最新到最旧
最古老=在最古老的顺序返回项目最新
为了标题的字母顺序标题=项目回报
为了网址的网站按字母顺序=返回项目

现在,我想要得到的视频数据,并通过最新排序。
但我在很短的线索如何在我的查询中添加这些参数。

下面是我做过尝试,但我得到 400错误的请求。不是也一定要放什么选择,因为我不知道这个数据库的表名。

  VAR最纯净=要求(最纯净)
,getpocket =新的最纯净({提供商:getpocket'})
getpocket.query()
  。选择('')
  。凡({contentType中:视频},{排序:'最新'})
  。员额('得到')
  .auth('XXXXX','XXXXX')
  .request(函数(ERR,RES体){
    的console.log(体);
  })


袖珍的API只接受POST请求,并希望你发送一个JSON EN codeD请求体:

  getpocket.query()
  。员额('得到')
  以.json({
    CONSUMER_KEY:'...',
    的access_token:'...',
    的contentType:'文章',
    排序:标题
  })
  .request(函数(ERR,资源,机构){})

通过此提供具体的查询API看起来有点不可思议,因为端点被称为 GET 和你正在一个 POST 要求它。

最纯净是建立在的申请,它是与它完全兼容。下面code会产生完全相同的结果为上述之一:

  getpocket.post('https://getpocket.com/v3/get',{
  JSON:{
    CONSUMER_KEY:'...',
    的access_token:'...',
    的contentType:'文章',
    排序:标题
  }
},功能(呃,资源,机构){})

另外,您可以使用申请来代替:

  VAR请求=要求(请求)
request.post('https://getpocket.com/v3/get',{
  标题:{内涵式:应用/ JSON的},
  身体:JSON.stringify({
    CONSUMER_KEY:'...',
    的access_token:'...',
    的contentType:'文章',
    排序:标题
  })
},功能(呃,RES体){
  的console.log(JSON.parse(体))
})

I'm trying to pass some parameters with my API request so I can get data that's already filtered.

I'm using a module called Purest and it's basically a REST API client library. It supports making expressive query call as per Purest Query API

And, my API provider is Pocket and their documentation says the following more info here.

contentType

article = only return articles
video = only return videos or articles with embedded videos
image = only return images

sort

newest = return items in order of newest to oldest
oldest = return items in order of oldest to newest
title = return items in order of title alphabetically
site = return items in order of url alphabetically

Now, I want to get video data and sort it by newest. But I'm short on clues how to add these parameters in my query.

Below is what I've tried but I'm getting 400 Bad Request. Not also sure what to put in select because I don't know the table name of this database.

var Purest = require('purest')
, getpocket = new Purest({provider:'getpocket'})


getpocket.query()
  .select('')
  .where({contentType:'video'},{sort:'newest'})
  .post('get')
  .auth('xxxxx', 'xxxxx')
  .request(function (err, res, body) {
    console.log(body);
  })

解决方案

Pocket's API accepts only POST requests, and expect you to send a JSON encoded request body:

getpocket.query()
  .post('get')
  .json({
    consumer_key:'...',
    access_token:'...',
    contentType:'article',
    sort:'title'
  })
  .request(function (err, res, body) {})

With this provider specifically the Query API looks a bit weird, because the endpoint is called get and you are making a POST request to it.

Purest is built on top of request and it's fully compatible with it. The following code will produce the exact same result as the above one:

getpocket.post('https://getpocket.com/v3/get', {
  json: {
    consumer_key:'...',
    access_token:'...',
    contentType:'article',
    sort:'title'
  }
}, function (err, res, body) {})

Alternatively you can use request instead:

var request = require('request')
request.post('https://getpocket.com/v3/get', {
  headers: {'content-type':'application/json'},
  body: JSON.stringify({
    consumer_key:'...',
    access_token:'...',
    contentType:'article',
    sort:'title'
  })
}, function (err, res, body) {
  console.log(JSON.parse(body))
})

这篇关于如何用最纯净的模块参数查询(node.js的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆