Retry Mechanism
To use Webhook Notification retry mechanism, you should return a response with HTTP Status Code other than 2xx
. This response will inform the Webhook Notification sender to retry the request as much as 10
times with intervals of 15
minutes.
If you're unfamiliar with HTTP Status Codes, we recommend reading this wikipedia article for reference.
- 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 (
"log"
"net/http"
)
func main() {
http.HandleFunc("/listener", func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusBadRequest)
})
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.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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 ResponseEntity<String> listener() {
return new ResponseEntity<String>(HttpStatus.BAD_REQUEST);
}
}
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.sendStatus(400));
app.listen(port, () => console.log(`Server is running!`));
Pay attention to this line res.sendStatus(400)
, this line of code determines what status code that you will send to us, which in this case is 400
or Bad Request
.
As defined above, Webhook Notification will understand HTTP status codes 2xx
as a successful notification. Other than that, will attempt to retry it 10
time in space of 15
minutes, or until your server returns a response with 2xx
status code. In summary, this code will notify us to retry the Webhook Notification.
To test the code above, using this command.
curl -X POST 'http://localhost:8080/listener' -i
It will show this message.
HTTP/1.1 400 Bad Request
Date: Tue, 30 Jun 2020 13:46:51 GMT
Content-Length: 1
Content-Type: text/plain; charset=utf-8
The first line HTTP/1.1 400 Bad Request
shows that it's status code is not 2xx
, which will prompt a retry to our server.
This means that you now know how to ask our Webhook Notification to retry, Congratulations!