title: javascript protocol buffer decode data from python server failed
body: <p>I am using javascript as client and a python as server.</p>
  
  <p>I need to send/receive in between them using protocol buffer.</p>
  
  <p>My proto looks like this :</p>
  
  <pre><code>message CMsgBase
  {
      required CMsgHead msghead           = 1;
      optional bytes msgbody              = 2;
  }
  
  message CMsgHead
  {
      required int32 msgtype                = 1;
      required int32 msgcode                = 2;
  }
  </code></pre>
  
  <p>I am using protobuf.js in javascript and is sending the data to server using XMLHttpRequest with a POST method :</p>
  
  <pre><code>xhr.send(pbMessage.encodeAB());
  </code></pre>
  
  <p>The server side can receive this message and decode it successfully:</p>
  
  <h1>BaseHTTPServer</h1>
  
  <pre><code>def do_POST(self):
      pbMessage = self.rfile.read(length) 
      # ...
  </code></pre>
  
  <p>The problem is that I can not decode the data in javascript side received from the server side.</p>
  
  <p>Here is how I send the data to client from server :</p>
  
  <pre><code>pbMessageReturn = CMsgBase()
  # ......
  self.send_response(200)
  self.end_headers()
  
  """Serializes the protocol message to a binary string.
  
  Returns:
    A binary string representation of the message if all of the required
    fields in the message are set (i.e. the message is initialized).
  """
  
  self.wfile.write(pbMessageReturn.SerializeToString())
  </code></pre>
  
  <p>And here is the result of print(pbMessageReturn) in server side :</p>
  
  <pre><code>msghead {
    msgtype: 10006
    msgcode: 1
  }
  msgbody: "\n\014192.168.1.16"
  </code></pre>
  
  <p>Everything seem s to be fine.</p>
  
  <p>And here is how I decode the message from server in javascript:</p>
  
  <pre><code>xhr.onreadystatechange = function() {
      if (xhr.readyState == 4) {
          var result = xhr.responseText;
          var recvMessage = CMsgBase.decode(result, "utf8");
      }
  };
  </code></pre>
  
  <p>And I am having an error:</p>
  
  <pre><code>protobuf.js:2335 Uncaught Error: Missing at least one required field for Message .CMsgBase: msghead(…)
  </code></pre>
  
  <p>BTW, if I try to send the back the data without serialize it:</p>
  
  <pre><code>self.wfile.write(pbMessageReturn)
  </code></pre>
  
  <p>The response I get in javascript is :</p>
  
  <pre><code>console.log(xhr.responseText);
  
  msghead {
    msgtype: 10006
    msgcode: 1
  }
  msgbody: "\n\014192.168.1.16"
  </code></pre>
  
  <p>I am really not sure if the error is in the server side or the client side.</p>
  
  <p>Any advice will be appreciated, thanks :)</p>