`import querystring from "querystring"` translating entire `body` as single Object key

in netlify function:

import querystring from "querystring";

...

  const params = querystring.parse(event.body);
  console.log(params);

but params logs as:

  [Object: null prototype] { '{\n\t"name": "Jane"\n}': '' }

Why is my entire body getting created as the key to this object?

Hi @JamesWholebear, That’s because you’re passing the body of the inbound request in to your querystring parser. You don’t actually need to manually parse the query params since they are available in an object on the event as event. queryStringParameters already.

Thanks for responding @futuregerald, I don’t seem to be able to do this on a POST request. I just get

[Object: null prototype] {}

I’m not sure what you’re printing there but it works for me in my tests:

image

And that’s using the following code in the function:

exports.handler = function (event, context, callback) {

    console.log(event.queryStringParameters);
    console.log(`This is the value of test: ${event.queryStringParameters.test}`);
    console.log(event)

    callback(null, {
        statusCode: 200,
        body: JSON.stringify(event)
    });
};

Thanks again, @futuregerald.

It looks like you sent your parameters in the headers. I’m sending mine through in this case through the body of a POST request, like this:

Do you know of a way to access what’s on the body of the POST?

Hi @JamesWholebear, if you look closely to the first screenshot that Gerald provided, you can see that the event objects has a body key. Similar to node/express, you can access the body of the incoming request via event.body. Let me know if you have any other questions.