hapi icon indicating copy to clipboard operation
hapi copied to clipboard

Parsing Raw Payloads

Open davidkavanaugh opened this issue 5 years ago • 4 comments

Support plan

n/a

  • is this issue currently blocking your project? (yes/no): yes
  • is this issue affecting a production system? (yes/no): no

Context

  • node version: 12.19.0
  • module version: 12.0.1
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): hapi application
  • any other relevant information:

How can we help?

My team is working on a project where we need to use a raw payload, as such:

payload: {
        parse: false,
},

However, since we also want to utilize schema validations, we're calling the .validate() method as such:

const validationSchema = Joi.object();
const result = validationSchema.validate(request.payload.toString());

My problem is that the .validate() method expects a JSON object. However, I'm unable to parse the full payload.

Does anyone know of a solution or have any ideas?

davidkavanaugh avatar Aug 25 '21 16:08 davidkavanaugh

I believe that if you put

const result = validationSchema.validate(JSON.parse(request.payload.toString()));

What you were sending was JSON text vs a JavaScript Object.

devinstewart avatar Aug 25 '21 21:08 devinstewart

I believe that if you put

const result = validationSchema.validate(JSON.parse(request.payload.toString()));

What you were sending was JSON text vs a JavaScript Object.

Thanks, @devinstewart. that was my thought too, but JSON.parse(request.payload.toString()) throws an error: "SyntaxError: Unexpected token O in JSON at position 535"

davidkavanaugh avatar Aug 30 '21 15:08 davidkavanaugh

So that tells me that the string being created by:

request.payload.toString()

Is not valid JSON. I would venture to guess that the Unexpected token O is the beginning of the word "Object".

Is there a chance you could provide a console.log of the string created? You can obfuscate the values, it's the keys I'm interested in.

devinstewart avatar Aug 30 '21 19:08 devinstewart

Oh @devinstewart, . thank you for pointing out my stupid mistakes. Yes, I was trying to parse an [Object].

good grief! and Thank you!!!!

davidkavanaugh avatar Sep 02 '21 14:09 davidkavanaugh