json_proto_response.go 472 B

123456789101112131415161718192021
  1. package ginutil
  2. import (
  3. "kpt-tmr-group/pkg/apierr"
  4. "kpt-tmr-group/pkg/apiok"
  5. "kpt-tmr-group/pkg/jsonpb"
  6. "net/http"
  7. "github.com/gin-gonic/gin"
  8. "github.com/golang/protobuf/proto"
  9. )
  10. func JSONResp(c *gin.Context, pb proto.Message) {
  11. bs, err := jsonpb.MarshalBytes(pb)
  12. if err != nil {
  13. apierr.AbortInternalError(c, http.StatusInternalServerError, err)
  14. return
  15. }
  16. c.JSON(http.StatusOK, apiok.CommonResponse(bs))
  17. //c.Data(http.StatusOK, "application/json", bs)
  18. }