3

I'm currently working with hapi and I've come across an issue that I can't seem to find any solution for or previous mention of. When I send the following request only my first query parameter is in the request.query object.

curl -H "Content-Type: application/json" -X PUT https://localhost:3000/lists/{username}/{listname}?token='token'&resource_id='resource_id'

with items in {} and '' replaced with actual names.

My route is currently written as such

server.route({
    method: 'PUT',
    path: '/lists/{username}/{listname}',
    handler: function(request, reply) {
        const username = encodeURIComponent(request.params.username);
        const listname = encodeURIComponent(request.params.listname);
        const resource_id = request.query.resource_id;
        const token = request.query.token;
        console.log(request.query);
        verify(token, username, {callback}, listname, resource_id, reply);
    }
});

The console.log call results in

{ token: 'token' }

If I do a console.log(resource_id) I get "undefined" in the console. hapi's documentation states that all query parameters should be found in the request.query object. For some reason this isn't happening. I've looked at the hapijs documentation, looked over my API call, and I've read people's examples of handling query params. Any idea what's going on here?

vitr
  • 6,766
  • 8
  • 30
  • 50
benjdj
  • 33
  • 5

3 Answers3

1

The issue is with your curl command not HapiJS.

Try running curl -H "Content-Type: application/json" -X PUT "https://localhost:3000/lists/{username}/{listname}?token=token&resource_id=resource_id"

It's the ampersand in the query string being interpreted as the end of the command. For an explanation as to why the command wasn't working see this questions answer

Community
  • 1
  • 1
cwurtz
  • 3,177
  • 1
  • 15
  • 15
  • Thank you! Kicking myself that I forgot shell sees "&" and runs the process in the background. – benjdj Aug 29 '16 at 05:23
0

The problem is with firing of curl request. There is & in the curl request and the URL is not in quotes; so the & becomes a shell modified to start a detached process. Your route is working fine, just fire the curl with URL in quotes

curl -H "Content-Type: application/json" -X PUT "https://localhost:3000/lists/{username}/{listname}?token='token'&resource_id='resource_id'"

Pankaj
  • 954
  • 8
  • 15
0

If you want to get the data from query params then you should do like in below code:

server.route({
    method: 'PUT',
    path: '/lists/{username}/{listname}',

    validate: {
        params: {
            username: Joi.string().min(1).max(15).required(),
            listname:Joi.string().min(1).max(15).required(),

        },
   query: {
            token:    Joi.number().integer().min(1).max(100).required(),
            resource_id: Joi.number().integer().min(1).max(100).required(),
         }
       },
    handler: function (request, reply) {
         const username = encodeURIComponent(request.params.username);
         const listname = encodeURIComponent(request.params.listname);
         const resource_id = request.query.resource_id;
         const token = request.query.token;
         console.log(request.query);
         verify(token, username, {callback}, listname, resource_id, reply);
         reply("Hello There..");
    }

You can use above code to get the params/query data.

Shubham Verma
  • 8,783
  • 6
  • 58
  • 79