Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json Custom output support (with examples) #1051

Closed
aadog opened this issue Sep 30, 2019 · 7 comments
Closed

json Custom output support (with examples) #1051

aadog opened this issue Sep 30, 2019 · 7 comments

Comments

@aadog
Copy link

aadog commented Sep 30, 2019

	gohook.Hook(runtime.ForwardResponseMessage, func(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, req *http.Request, resp proto.Message, opts ...func(context.Context, http.ResponseWriter, proto.Message) error) {
		bt,err:=marshaler.Marshal(resp)
		if err!=nil{
			return
		}
		rmsg:=make(map[string]interface{})
		err=marshaler.Unmarshal(bt,&rmsg)
		if err!=nil{
			return
		}
		rmsg["err"]=0
		rmsg["msg"]="r ok"
		newbt,err:=marshaler.Marshal(rmsg)
		if err!=nil{
			return
		}
		w.Write([]byte(newbt))
		w.WriteHeader(200)
	},nil)
  {"Msg":"state ok","err":0,"msg":"r ok"}

Is it possible to add runtime.ForwardResponseMessage here to support custom format json, such as "err": 0, "msg": "r ok", or convert them all to lowercase or uppercase This should be the right choice

@johanbrandhorst
Copy link
Collaborator

I'm not sure I understand the question. As you've done in this example, you can use ForwardResponseMessage to configure the output. If you're unhappy with the JSON mapping of your field names, you need to go back to your proto files or your marshaller configuration. You maybe using OrigName: true/false when you don't want to.

If you need more help, please pop by the #grpc-gateway channel on Gophers slack and I can maybe help you understand what you're asking.

@aadog
Copy link
Author

aadog commented Sep 30, 2019

Thank you for your answer. The current question is that ForwardResponseMessage function can not be replaced dynamically. It can only be implemented by hook. How should I replace ForwardResponseMessage dynamically?

@johanbrandhorst
Copy link
Collaborator

I suppose you could call another function in it which you can reassign to dynamically? It sounds like a pretty bad idea. What are you trying to do? I still think this would be easier to handle on slack.

@aadog
Copy link
Author

aadog commented Sep 30, 2019

Implement runtime.WithProtoErrorHandler

runtime.WithForwardResponseMessage similar function, I saw a night yesterday seems to need to change the main code or gohook can achieve the effect

@aadog
Copy link
Author

aadog commented Sep 30, 2019

If you use runtime.WithForwardResponseOption(), multiple returns will be said.

{"Error": 0, "Error": "Runs normally", } {// pbret}

@aadog
Copy link
Author

aadog commented Sep 30, 2019

kf

Add the interception function style of runtime.ForwardResponseMessage here to support the custom json format.

@bourne-3
Copy link

kf

Add the interception function style of runtime.ForwardResponseMessage here to support the custom json format.

i use this way to deal,but the response show like below

{
    "code": 0,
    "msg": "success",
    "data": {
        "isSucc": false
    }
}{
    "isSucc": false
}

how to eliminate the

{
    "isSucc": false
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants