Creating a Server
Basic POST Request Handler
To handle Webhook Notification messages, you would need a server that can handle POST
requests since Webhook Notification will come as a POST
request. To do that, this tutorial will provide all the necessities to create a server that handles POST
request and understands the payload received from us.
- Go
- Java
- Node.js
This Golang example uses Golang Standard Library, if you’re unfamiliar with how Golang works, we recommend reading the official documentation here.
import (
"io"
"log"
"net/http"
)
func main() {
http.HandleFunc("/listener", func(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "Hello, world!")
})
log.Print("Server is running!")
log.Fatal(http.ListenAndServe(":8080", nil))
}
This Java example uses Spring Boot, if you’re unfamiliar with how Spring Boot works, we recommend reading the official documentation here.
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@PostMapping("/listener")
public String listener() {
return "Hello, World!";
}
}
This Node.js example uses Express.js framework, if you’re unfamiliar with how Express.js framework works, we recommend reading the official documentation here.
const express = require('express');
const app = express();
const port = 8080;
app.post('/listener', (req, res) => res.send('Hello, World!'));
app.listen(port, () => console.log('Server is running!'));
From the example above, We have successfully created a server to handle all the messages sent to the /listener
path. We should try running it.
- Go
- Java
- Node.js
go run main.go
After running the command shown above you should see this message.
2020/06/30 19:02:29 Server is running!
Where the numbers on the left is the time our server prints the log.
mvn spring-boot:run
After running the command shown above you should see this message in the last line.
2020-07-01 22:50:15.170 INFO 71129 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 0.867 seconds (JVM running for 1.045)
Where the numbers on the left is the time our server prints the log.
node app.js
After running the command shown above you should see this message.
Server is running!
It means that the server is up and running.
And if you see the message like shown above, it means that your server is up and running. So then let’s continue to test it by running this command below.
curl -X POST 'http://localhost:8080/listener'
We should see a Hello, World!
message.
Consuming JSON message
After successfully creating a web server to listen to Webhook Notification, you would have to understand the message received. Webhook Notification sends messages with JSON format. As for this example, we will guide you creating a Chat Notification payload which can be found here and then extract the field message
and print it.
- Go
- Java
- Node.js
This Golang example uses Golang Standard Library, if you’re unfamiliar with how Golang works, we recommend reading the official documentation here.
import (
"io"
"log"
"net/http"
"encoding/json"
)
type ChatNotificationPayload struct {
MsgID int64 `json:"msg_id"`
BuyerID int64 `json:"buyer_id"`
Message string `json:"message"`
Thumbnail string `json:"thumbnail"`
FullName string `json:"full_name"`
ShopID int64 `json:"shop_id"`
}
func main() {
http.HandleFunc("/listener", func(w http.ResponseWriter, req *http.Request) {
var result ChatNotificationPayload
json.NewDecoder(req.Body).Decode(&result)
io.WriteString(w, result.Message)
})
log.Print("Server is running!")
log.Fatal(http.ListenAndServe(":8080", nil))
}
This Java example uses Spring Boot, if you’re unfamiliar with how Spring Boot works, we recommend reading the official documentation here.
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import com.fasterxml.jackson.annotation.JsonProperty;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@PostMapping("/listener")
public String hello(@RequestBody ChatNotificationPayload body) {
return body.message;
}
}
class ChatNotificationPayload {
@JsonProperty("msg_id")
int msgId;
@JsonProperty("buyer_id")
int buyerId;
@JsonProperty("message")
String message;
@JsonProperty("thumbnail")
String thumbnail;
@JsonProperty("full_name")
String fullName;
@JsonProperty("shop_id")
int shopId;
}
This Node.js example uses Express.js framework, if you’re unfamiliar with how Express.js framework works, we recommend reading the official documentation here.
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 8080;
app.use(bodyParser.json());
app.post('/listener', (req, res) => {
res.send(req.body.message);
})
app.listen(port, () => console.log(`Server is running!`));
We can test the above code using this command below.
curl -X POST 'http://localhost:8080/listener' -d '{"message":"Hello, Tokopedia!"}'
After executing the command above, you should see Hello, Tokopedia!
. And when you do, it means that you have successfully implemented a simple Webhook Notification Listener, Congratulations!.