Where would I find the payload with a Wix webhook? - node.js

I have a couple Wix webhooks pointed to a couple routes on my server. The webhook is in fact hitting my routes, but I'm not seeing the payload anywhere. The documentation says it's in the form of a JWT sent in the body of the request. The body is empty though. Am I missing something?
Here is my route receiving the request:
router.post("/app-installed", (req, res) => {
console.log("req.body: ", req.body);
console.log("headers", req.headers);
res.sendStatus(200);
});

Related

Access URL query Params in an Express POST route

I have a NodeJS/Express application.
From an url endpoint titled: localhost:3000/form?someProp=someValue&somethingElse=someOtherValue
This page submits a form to another Express endpoint.
I submit to an Express POST endpoint. I know in a GET endpoint I could access the query params via req.query, but I cannot seem to do that in a POST request.
Is there a way to access the query params from the request in a POST route?
(other than splicing the header.referrer... which I may just have to do)
Here are some code snippets:
If I submit to the first route it works, if I submit to the second... it does not.
router.get('/test',
(req, res) => {
console.log(req.query); // {someProp: someValue, somethingElse: someOtherValue }
}
);
router.post('/test2',
(req, res) => {
console.log(req.query); // returns {}
}
);
So I tried to send a simple request to test it and got everything working without doing anything special (The only thing extra I have is that I'm using body-parser middleware from npm):
app.use(bodyParser.json({limit: '50mb'}));
Then I tried this simple route and got the result query params as you can see in the picture attached.
http://localhost:8080/test?test=1&what=2
Any chance you're sending the other form request from the client in a different way? try looking at the network in chrome and see if you sending what you expecting. Obviously, there is something missing here as it worked for me without doing anything special.

How to get req.body from Facebook's data deletion URL call?

I've implemented the "sign in with Facebook" authorization method for my Express.js Node app. Since the app is hosted in the EU, Facebook requires it to have a data deletion request callback URL.
I've created an endpoint for the data deletion request, but whenever I make Facebook call that endpoint both req.body and req.query are empty - if I've understood correctly the body should contain a signed_request that could be used to e.g. verify the caller.
My CORS settings should be fine and I've tested my endpoint by calling it from Postman. The endpoint is able to receive a POST request with a JSON body without any problem.
So, what am I doing wrong - why does it seem like Facebook is calling my endpoint with a POST request that has an empty body?
My endpoint:
import express from 'express'; // 4.17.1
const router = express.Router();
router.post('/fb_data_deletion', (req, res, next) => {
console.log(req.body); // {}
console.log(req.query); // {}
if (!req.body || !req.body.signed_request) {
console.log('Bad request'); // Ends up here whenever Facebook calls this route
return req.sendStatus(400);
}
// verify request, delete user's data + other code here
});
Turns out Facebook isn't sending a POST request that uses Content-Type application/json but application/x-www-form-urlencoded.
To get the body of Facebook's POST request I had to add the following line to my app.js where the Node server is being set up:
app.use(express.urlencoded());

Nodejs doesnt receive jsondata

My Node.js server is not able to send the data it receives back to the browser. It is able to send self-made data like this
app.post('/login', (req,res) =>{
console.log(req.body);
res.setHeader('Content-Type', 'application/json');
res.json({test: "test info",test2: "still testing"});//works
//res.json(req.body);//does not work
})
In the fetch POST request, I have set the header 'Content-Type': 'application/json'.
In the firefox debugger, I can see that the information is sent via POST.
I have tried stuff like
res.json(req.body);
res.json(req.body.pass);
res.json({pass:+req.body.pass});//response in browser -> {"pass":null}
res.json("{pass:"+req.body.pass+"}");//response in browser -> "{pass:undefined}"
Could someone tell me why the Node.js server doesn't receive the JSON data?
Have you installed the body-parser
? Otherwise express wont convert the body of the POST request to a JSON object that you can access with req.body.pass

Get Data from POST request on client side

I made a form post request from my react front end. In my express route I handle it, and I send the data to the browser, as such:
app.post("/api/fetchuser", async (req, res) => {
// Doing some stuff here
res.send(req.body.user);
});
My question is: How can I get the data from this POST request route back to my client side? Previously, I did axios.get(...) to retrieve data on a app.get(...) route in a similar fashion, however, this is a POST route. How can I solve this issue?
Just do axios.post request. See Performing a POST request section of axios documentation.
Edit: Yes you can also retrieve data by POST request. You don't have to post any data to the server while performing the request.
axios.post('/api/fetchuser/',{})
.then(res => console.log(res.data));

Test nodejs using postman: How to get req.user in postman

I'm using passport for register & login function. My API is like this:
router.post('/secret', ctr);
(req, res, next) => {
if (req.user) {
...
}
}
How to test this API using postman?
Your question is not clear. First refer to the postman documentation.
If what you are trying to achieve is to send in the "user" field properly then in Postman set the method to POST, and Content-Type to application/json and send in a valid JSON body with a "user" field in it with your request.
Your Node app should have body-parser installed as well.

Resources