benjdj benjdj - 1 year ago 160
Javascript Question

Hapijs query parameters undefined

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
replaced with actual names.

My route is currently written as such

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;
verify(token, username, {callback}, listname, resource_id, reply);

call results in

{ token: 'token' }

If I do a
I get "undefined" in the console. hapi's documentation states that all query parameters should be found in the
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?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download