Git Product home page Git Product logo

Comments (2)

lonetrail avatar lonetrail commented on September 27, 2024
  • option 1: set the "Accept" header to "application/xml" on client side

    $ curl -X GET 'localhost:8000/helloworld/a' -H "Accept: application/xml"
    <HelloReply><Message>Hello a</Message></HelloReply>%
  • option 2: change the request header on server side in somewhere like httpFilter or middleware or service method.

// server/http.go
package server

import (
	"context"
	v1 "xmldemo/api/helloworld/v1"
	"xmldemo/internal/conf"
	"xmldemo/internal/service"

	stdhttp "net/http"

	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware"
	"github.com/go-kratos/kratos/v2/middleware/recovery"
	"github.com/go-kratos/kratos/v2/transport"
	"github.com/go-kratos/kratos/v2/transport/http"
)

// NewHTTPServer new an HTTP server.
func NewHTTPServer(c *conf.Server, greeter *service.GreeterService, logger log.Logger) *http.Server {
	var opts = []http.ServerOption{
		http.Filter(
			// handle in filter
			func(h stdhttp.Handler) stdhttp.Handler {
				return stdhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
					if r.URL.Path == "helloworld/a" {
						r.Header.Set("Accept", "application/xml")
					}
					h.ServeHTTP(w, r)
				})
			},
		),
		http.Middleware(
			// handle in middleware
			func(h middleware.Handler) middleware.Handler {
				return func(ctx context.Context, req interface{}) (interface{}, error) {
					transporter, ok := transport.FromServerContext(ctx)
					if ok {
						if transporter.Operation() == "/helloworld.v1.Greeter/SayHello" {
							transporter.RequestHeader().Set("Accept", "application/xml")
						}
					}

					return h(ctx, req)
				}
			},
			recovery.Recovery(),
		),
	}
	if c.Http.Network != "" {
		opts = append(opts, http.Network(c.Http.Network))
	}
	if c.Http.Addr != "" {
		opts = append(opts, http.Address(c.Http.Addr))
	}
	if c.Http.Timeout != nil {
		opts = append(opts, http.Timeout(c.Http.Timeout.AsDuration()))
	}
	srv := http.NewServer(opts...)
	v1.RegisterGreeterHTTPServer(srv, greeter)
	return srv
}
 
// service/greeter.go
package service

import (
	"context"

	v1 "xmldemo/api/helloworld/v1"
	"xmldemo/internal/biz"

	"github.com/go-kratos/kratos/v2/transport"
)

// GreeterService is a greeter service.
type GreeterService struct {
	v1.UnimplementedGreeterServer

	uc *biz.GreeterUsecase
}

// NewGreeterService new a greeter service.
func NewGreeterService(uc *biz.GreeterUsecase) *GreeterService {
	return &GreeterService{uc: uc}
}

// SayHello implements helloworld.GreeterServer.
func (s *GreeterService) SayHello(ctx context.Context, in *v1.HelloRequest) (*v1.HelloReply, error) {
	// handle in method
	transporter, ok := transport.FromServerContext(ctx)
	if ok {
		transporter.RequestHeader().Set("Accept", "application/xml")
	}

	g, err := s.uc.CreateGreeter(ctx, &biz.Greeter{Hello: in.Name})
	if err != nil {
		return nil, err
	}

	return &v1.HelloReply{Message: "Hello " + g.Hello}, nil
}

from kratos.

kissIce avatar kissIce commented on September 27, 2024

it's so helpful, thk

from kratos.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.