grpcurl icon indicating copy to clipboard operation
grpcurl copied to clipboard

Any encoding of the symbols?

Open yiyue115 opened this issue 4 years ago • 4 comments

Hi,

I have a struct in this format

type Response struct {
	Url            string   `protobuf:"bytes,1,opt,name=url,json=url,proto3" json:"url,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

I set it as

return &Response{
        Url: "https://abcd.com/&abcd"
}

When I make the request, I got this response

{
  "url": "https://abcd.com/\u0026abcd"
}

The \u0026 should be & here but I don't know why it appears to be encoded. I wonder whether that's what grpcurl does? Because I haven't found anything with my code which does the encoding...

yiyue115 avatar Sep 04 '20 01:09 yiyue115

In JSON `'\u0026' is the same as '&'; the former is unicode escaped.

I dug a little bit: grpcurl is not doing any encoding explicitly, but it uses the "encoding/json" Go package (part of Go standard runtime). That package apparently chooses to escape &, <, and > by default, in case the JSON data is going to be rendered into an HTML page.

A change to fix this is fairly straight-forward.

jhump avatar Sep 04 '20 13:09 jhump

FYI, until this is fixed, that is still valid JSON and any/all consumers of it will know how to correctly unescape that character.

So if you pipe the JSON to jq, you'll see that it successfully unescapes the value.

jhump avatar Sep 04 '20 13:09 jhump

Hi Joshua,

Thanks so much for the timely response!! Sorry I'm still a bit new to Golang

Currently I'm using grpcurl to test my endpoint. Are you saying that I'd get the same response as \u0026 but I might need to do some marshalling on it if I wanna use it on my frontend? Or you're saying that this is only an issue with encoding/json and I should be able to ignore it if using it on my own app?

Thanks again

yiyue115 avatar Sep 05 '20 05:09 yiyue115

but it uses the "encoding/json" Go package (part of Go standard runtime).

Also, is it possible if we could use an option not to escape these?

yiyue115 avatar Sep 05 '20 05:09 yiyue115