Catch webhook node.js

I am trying to catch a PUT / webhook request that is created by the Aftership API in node.js. A PUT request is made every time a push notification is required, I use Parse to send notifications, but I need some data from the webhook.

The webhook header looks like Content-Type: application/json and contains the following data:

ts - UTC unix timestamp that an event has occurred

event - the name of the event (the value will be 'Tracking_update' to track the update)

msg - information about the message for which the event occurred, in the following format.

How can I get the tracking number, slug and value for token in the custom fields dictionary in node or js?

 { "event": "tracking_update", "msg": { "id": "53aa94fc55ece21582000004", "tracking_number": "906587618687", "title": "906587618687", "origin_country_iso3": null, "destination_country_iso3": null, "shipment_package_count": 0, "active": false, "order_id": null, "order_id_path": null, "customer_name": null, "source": "web", "emails": [], "custom_fields": {}, "tag": "Delivered", "tracked_count": 1, "expected_delivery": null, "signed_by": "D Johnson", "shipment_type": null, "tracking_account_number": null, "tracking_postal_code": "DA15BU", "tracking_ship_date": null, "created_at": "2014-06-25T09:23:08+00:00", "updated_at": "2014-06-25T09:23:08+00:00", "slug": "dx", "unique_token": "xk7LesjIgg", "checkpoints": [{ "country_name": null, "country_iso3": null, "state": null, "city": null, "zip": null, "message": "Signed For by: D Johnson", "coordinates": [], "tag": "Delivered", "created_at": "2014-06-25T09:23:11+00:00", "checkpoint_time": "2014-05-02T16:24:38", "slug": "dx" }] }, "ts": 1403688191 } 
+5
source share
3 answers

This can be done using the Express framework, for example:

 var express = require('express'), bodyParser = require('body-parser'), app = express(), port = 3000; app.use(bodyParser.json()); app.post('/', function (req, res) { var body = req.body; var trackingNumber = body.msg.tracking_number; var slug = body.msg.slug; var token = body.msg.unique_token; console.log(trackingNumber, slug, token); res.json({ message: 'ok got it!' }); }); var server = app.listen(port, function () { var host = server.address().address var port = server.address().port console.log('Example app listening at http://%s:%s', host, port) }); 

Here is the GIT repository , just clone it and do npm install , and then npm start . The server will run on port 3000 : D

Note. In the Aftership Webhook documentation, I saw that they would request the HTTP POST method, not the PUT method, so I am creating an example POST request. Just replace it with PUT if you want it to catch the PUT request.

+2
source

To check webhooks data, I would suggest storing each request in a database and then query the database. Since each request is different, the easiest way would be to create an API in sails.js (Node.js framework with an easy to use ORM).

 sudo npm install sails -g sails new _project_name_ cd _project_name_ sails generate api Records 

Using the last command, the sails created a controller and model for storing your web hosting data.

I suggest installing pm2 to run the application. you can run it with

 pm2 start app.js 

Then you should set up your web host in Aftership for the following URL:

YOUR_SERVER_IP: PORT / records / create

You can check the data at the following URL:

YOUR_SERVER_IP: PORT / Reports / find

if you want to analyze the data, this can be done in RecordController.js, for example:

 Parsing: function(req, res) { Records.find({}).exec(function(err, results) { var output = []; while (results.length) { var result = results.pop(); //custom parsing goes here //example: output.push({ tracking_number: result.msg.tracking_number, slug: result.msg.slug, unique_token: result.msg.unique_token }); } return res.json(output); }); }, 

You can call this method with the following URL: YOUR_SERVER_IP: PORT / records / Syntax

I created a project in Codeanywhere to demonstrate

Webhook endpoint: http://port-1337.zavtt4t8a0jm7vigncyo3txxmuhxgvix3yxk66pvydgqfr.box.codeanywhere.com/records/create

To check the data just replace / create part of the url for / find

git repo is here: https://github.com/dkatavic/webhook_for_aftership

you can just clone the project on your server and run it (or use my server for testing)

+2
source

You can get a PUT request with

 app.put('/someRouteToCatchWebHook', function(request, response) { //webhook parsing goes here }); 

(I'm sure you use expressjs in your code - see http://expressjs.com/api.html#app.METHOD for more details).

If the webhook data is in the request body, you can use the https://www.npmjs.com/package/body-parser module to parse it.

+1
source

Source: https://habr.com/ru/post/1210708/


All Articles