Search code examples
node.jsrestexpressnode-rest-client

Multiple path parameters are not working in node-rest-client


I'm using node-rest-client library on the portal side of my project to call my rest APIs.

I have the following registered Method on the portal side:

client.registerMethod("addFriendToUser", host + "/users/&{userId}/friends/${friendId}", "POST");

When I call it on the following way with two string parameters, only one of them will appear on the api-side:

var args = {
    path: {
        'userId':   '1',
        'friendId': '2'
    },
    headers: {
        "Content-Type": "application/json",
        "Authorization": /* ... */
    }
};

client.methods.addFriendToUser(args, function(data, response) {
    // ...
});

On the API side I have the following endpoint:

// ...
var router = express.Router();

router.route('/users/:userId/friends/:friendId')
    .post(/* ... middleware to check the Auth token from the header */, function(req, res, next) {
        // ...
        // Debugging the request parameters
        console.log(req.params);
        // ...
    });

In this case I will see the following parameters in the req.params:

{
    userId: '&{userId}', 
    friendId: '1' 
}

It seems that my first parameter doesn't get forwarded to the endpoint. If I call the endpoint directly from POSTMAN it works fine. I also tried to switch the parameters but same result.

And I also checked the source code on github but cannot figure it out what could be the problem:

Any help will be appreciated :)


Solution

  • you have a syntax error:

    client.registerMethod("addFriendToUser", host + "/users/&{userId}/friends/${friendId}", "POST");
    

    should be:

    client.registerMethod("addFriendToUser", host + "/users/${userId}/friends/${friendId}", "POST");
    

    Note the misplaced & where $ is required