Postprocessor

By default, RPC response objects are format into a JSON string, and returned as API response.

Postprocessors handle responses from backend service. You can change default behavior by assigning a postprocessor.

Let’s change the response of API “/eat_apple/{num:[0-9]+}”:

Edit “yourservice/grpcapi/component/components.go”:

func (i *ServiceInitializer) InitService(s turbo.Servable) error {
+       s.RegisterComponent("postEatApple", postEatApple)
}

+var postEatApple turbo.Postprocessor = func (resp http.ResponseWriter, req *http.Request, serviceResp interface{}, err error) error {
+      resp.Write([]byte("[this is from postprocesser]"))
+  return nil
+}

Edit “yourservice/service.yaml”:

+postprocessor:
+  - GET /eat_apple/{num:[0-9]+} postEatApple

Restart HTTP server and test:

$ curl -w "\n" "http://localhost:8081/eat_apple/5"
[this is from postprocesser]{"message":"Good taste! Apple num=5"}