express parsing all request to json

守給你的承諾、 提交于 2021-01-27 18:54:55

问题


I am started to implement some API to my marketplace. I am using Shoppy.gg for get some cryptopayments. And i have got a problem about the verify their callbacks from my back end.

In their documentation explain to how validate shoppy with php:

$signature = hash_hmac('sha512', file_get_contents('php://input'), 'secret');
$is_valid = hash_equals($signature, $_SERVER['HTTP_X_SHOPPY_SIGNATURE']);

And i am trying to do it with nodejs express module

  const signature =  req.header('x-shoppy-signature')
  const verifySignature = sha512(JSON.stringify(req), shoppyWebhookSecret)
  if(signature !== verifySignature) return res.json({status: "error", message: "payment-hash-wrong"})

But in my code, ivgot an exception about Converting circular structure to JSON. Am i understand this documentation wrong ? Shouldnt i create sha512 hash from request by using my secret key ?

Edit: Also i thought maybe it can be gave url encoded string in php and i do it with javascript as coded following:

var verifySignature = sha512(new URLSearchParams(req.body).toString(), shoppyWebhookSecret)

but doesnt works too.

The documentation: https://shoppy.dev/#/webhooks


回答1:


JSON.stringify(req) is not the equivalent to file_get_contents('php://input'). req is a complex object that cannnot be represented as json. That is why you are getting the error Converting circular structure to JSON.

You need to find the equivalent to file_get_contents('php://input') in node.js. This should be the full request-body.



来源:https://stackoverflow.com/questions/65522372/express-parsing-all-request-to-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!