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

Returning a primitive type as a response instead of proto messages #994

Closed
dayadev opened this issue Aug 13, 2019 · 12 comments
Closed

Returning a primitive type as a response instead of proto messages #994

dayadev opened this issue Aug 13, 2019 · 12 comments

Comments

@dayadev
Copy link

dayadev commented Aug 13, 2019

I am having a use case if returning a primitive type as response even though the underlying grpc doesn't support returning primitive types as a response.

Wondering if its possible that I can return just a string as a response using grpc-gateway for below example rpc

rpc Echo(EchoReq) returns (EchoRes){
        option (google.api.http) = {
            get: "/api/echo/{message}"
        };
    }
  message EchoReq{
    string message=1;
  }
 message EchoRes{
    string echomessage=1;
}

Is it possible for the REST endpoint to just return string response without the the echomessage key?

@johanbrandhorst
Copy link
Collaborator

Could you show the exact JSON output you want?

@dayadev
Copy link
Author

dayadev commented Aug 13, 2019

Could you show the exact JSON output you want?

Its just a string without any keys more like html text.
Like when I hit the Rest endpoint/api/echo/welcome on a web browser, it should render the response html string on the browser. Response string is something like below


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test Page</title>
</head>
<body>
<p>test html body </p>
</body>

</html>

@johanbrandhorst
Copy link
Collaborator

You can try using the google.api.HttpBody type which gives you complete control over the returned data. See https://grpc-ecosystem.github.io/grpc-gateway/docs/httpbody.html. I think that should cover your needs.

@dayadev
Copy link
Author

dayadev commented Aug 14, 2019

You can try using the google.api.HttpBody type which gives you complete control over the returned data. See https://grpc-ecosystem.github.io/grpc-gateway/docs/httpbody.html. I think that should cover your needs.

That returns HttpBody as raw binary instead of actual html on a browser

@achew22
Copy link
Collaborator

achew22 commented Aug 14, 2019

Can you upload a sample program that exhibits that behavior? That would be a great test case for us to add. Thanks!

@dayadev
Copy link
Author

dayadev commented Aug 14, 2019

Can you upload a sample program that exhibits that behavior? That would be a great test case for us to add. Thanks!

Sure, the standard example when I added below option to mux gruntime.WithMarshalerOption(gruntime.MIMEWildcard, &gruntime.JSONPb{OrigName: false, EmitDefaults: true})

https://grpc-ecosystem.github.io/grpc-gateway/docs/httpbody.html

@johanbrandhorst
Copy link
Collaborator

The docs say that you need to use the runtime.HTTPBodyMarshaler (https://godoc.org/github.com/grpc-ecosystem/grpc-gateway/runtime#HTTPBodyMarshaler) when starting the runtime, are you doing that?

@johanbrandhorst
Copy link
Collaborator

The code you shared uses the JSONPb marshaler.

@dayadev
Copy link
Author

dayadev commented Aug 14, 2019

The docs say that you need to use the runtime.HTTPBodyMarshaler (https://godoc.org/github.com/grpc-ecosystem/grpc-gateway/runtime#HTTPBodyMarshaler) when starting the runtime, are you doing that?

Using this will return the original proto names something like prod_name instead I am trying to set the OrigName=false and return camel case prodName

@dayadev
Copy link
Author

dayadev commented Aug 14, 2019

The code you shared uses the JSONPb marshaler.

The internal implementation also uses JSONPb


// SetHTTPBodyMarshaler overwrite the default marshaler with the HTTPBodyMarshaler
func SetHTTPBodyMarshaler(serveMux *ServeMux) {
	serveMux.marshalers.mimeMap[MIMEWildcard] = &HTTPBodyMarshaler{
		Marshaler: &JSONPb{OrigName: true},
	}
}

@johanbrandhorst
Copy link
Collaborator

The HTTPBodyMarshaler embeds the Marshaler interface. Use m := runtime.HTTPBodyMarshaler{&runtime.JSONPb{}} and set whatever properties you want on the inner marshaller. Also, these kind of questions are better answered in the slack channel, please try there before raising an issue in the future.

@captainhorsepower
Copy link

Have a look at google/protobuf/wrappers.proto, there are StringValue, BoolValue, etc. protos

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

4 participants