2

The goal: When I open localhost:9000/xml page in the browser I would like to see xml content (which is large). I use gRPC server with grpc-gateway as a proxy.

To send my xml file I use server-side stream and send google.api.HttpBody message with text/xml content-type. Below is a part of my server code for streaming xml:

reader := bytes.NewReader(data) // "data" is a large xml file
buffer := make([]byte, 1024)

for {
    n, err := reader.Read(buffer)
    if err == io.EOF {
        break
    }
    if err != nil {
        return status.Error(codes.Internal, "")
    }

    if err := stream.Send(&httpbody.HttpBody{
        ContentType: "text/xml",
        Data:        buffer[:n],
    }); err != nil {
        return status.Error(codes.Internal, "")
    }
}

Unfortunately when I open localhost:9000/xml in the browser I see that my xml response is broken. Every 1025 byte starts on a new line, for example:

...
...
<loc>https://example.com/books/1</l
        oc>
...
...

How can I stream a large xml content and not break it?

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Klimbo
  • 1,308
  • 3
  • 15
  • 34
  • With this code, you are sending many httpbody messages, not one. Do not stream it and set the max msg size to something that will accept your XML data. – Burak Serdar Apr 02 '21 at 14:53
  • I have done it this way for now (I've increased sending size limit). You are right, I send many messages, but in DevTools there is only one response. – Klimbo Apr 02 '21 at 15:03

0 Answers0