0

I'm using rest.js from npm install rest and cannot get my params to append to the path in the request. Here is my client.js

'use strict';

var rest = require('rest');
var defaultRequest = require('rest/interceptor/defaultRequest');
var mime = require('rest/interceptor/mime');
var uriTemplateInterceptor = require('rest/interceptor/template');
var errorCode = require('rest/interceptor/errorCode');
var baseRegistry = require('rest/mime/registry');

var registry = baseRegistry.child();

registry.register('text/uri-list', require('./api/uriListConverter'));
registry.register('application/hal+json', require('rest/mime/type/application/hal'));

module.exports = rest
        .wrap(mime, { registry: registry })
        .wrap(uriTemplateInterceptor)
        .wrap(errorCode)
        .wrap(defaultRequest, { headers: { 'Accept': 'application/hal+json' }});

I used the template interceptor suggested here https://github.com/cujojs/rest/blob/master/docs/interceptors.md#module-rest/interceptor/template. Here is my code making the request:

client({method: 'GET', path: 'api/questions/page', params: {offset: 0, limit:10}})
        .then(response => console.log(response.request.path));

The output of the console.log(response.request.path) is api/questions/page and it also doesn't show the request params in the network log. How do I get the params to append to the path?

iurii
  • 4,142
  • 2
  • 23
  • 28
gary69
  • 3,620
  • 6
  • 36
  • 50
  • Have a look here https://stackoverflow.com/questions/6912584/how-to-get-get-query-string-variables-in-express-js-on-node-js – Robert I Dec 01 '17 at 23:25
  • I think that is using a different library, the link I posted above says the template interceptor should automatically append the params to the path but it isn't working – gary69 Dec 01 '17 at 23:28

1 Answers1

0

I just created my own interceptor

define(function(require) {
    'use strict';

    var interceptor = require('rest/interceptor');

    return interceptor({
        request: function (request /*, config, meta */) {
            /* If the URI is a URI Template per RFC 6570 (http://tools.ietf.org/html/rfc6570), trim out the template part */
            if (request.path.indexOf('{') === -1) {
                if(request.params != null){
                    request.path = request.path + '?'
                    Object.keys(request.params).forEach(function eachKey(key){
                        request.path = request.path + key + '=' + request.params[key] + '&'; 
                    });
                    request.path = request.path.slice(0, -1);
                }
                return request;
            } else {
                request.path = request.path.split('{')[0];
                return request;
            }
        }
    });

});
gary69
  • 3,620
  • 6
  • 36
  • 50