How to query with parameters using Purest module (

2019-09-11 11:01发布

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);
  })

标签: node.js api http
1条回答
Ridiculous、
2楼-- · 2019-09-11 11:29

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))
})
查看更多
登录 后发表回答