Sending JSON string as payload leads to loss of data
See original GitHub issueHello!
I’m experiencing some weird behaviour when sending a string in the JSON format in an RPC call. I have an RPC client on my broker that instructs my subscriber to execute a rest request, which is does fine. But when the subscriber goes to return the result of the rest request as the payload, the broker/rpcclient does not receive the entire json string, only one of the properties, in this case a string array.
Example code is supplied below, and there should be no code omitted which could interfere with the order of the methods shown below, apart from the log statements that have been removed.
I have this object on both the broker and the subscriber (using RestSharp for HttpStatusCode).
public class CustomRestResponse
{
public string Content { get; set; }
public HttpStatusCode StatusCode { get; set; }
public bool IsSuccessful { get; set; }
}
On the subscriber I execute the following code when returning the result from the rest request
public async void PublishMessage()
{
/* Code Abbreviated */
/* This would be passed as parameter to PublishMessage by the method executing the rest request*/
/*logging value before serialisation here*/
var objectToSerialize = new CustomRestResponse()
{
Content = "[ \"item1\" , \"item2\", \"item3\" ]",
HttpStatusCode = HttpStatusCode.OK,
IsSuccessful = true
};
var payload = JsonConvert.SerializeObject(objectToSerialize);
/*logging value after serialisation here*/
/*
This is where the fun begins.
The payload string looks just as we would expect.
However, when our RPCClient receives the response, the byte array only holds the value of the Content Property.
*/
var msg = new MqttApplicationMessageBuilder()
.WithTopic(topic)
.WithPayload(payload)
.Build();
await _client.PublishAsync(msg);
}
When the broker/rpcclient receives the response from the RPC it goes through the following code
/* also abbreviated*/
var piId = 1117;
var cmdString = $"{piId}";
var jsonString = JsonConvert.SerializeObject(commandObject);
var byteLoad = Encoding.UTF8.GetBytes(jsonString);
var byteRes = await _rpcClient.ExecuteAsync(
TimeSpan.FromSeconds(60),
cmdString,
byteLoad,
MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
);
var retStr = Encoding.UTF8.GetString(byteRes);
/*logging value of payload after getting from bytes*/
var jsonResponse = JsonConvert.DeserializeObject<CustomRestResponse>(retStr); //Throws exception because retStr is not a CustomRestResponse now.
/*retStr only contains the Content string property. */
What happens here is that the retStr variable only contains the content json array defined in the CustomRestResponse and nothing else, even though it gets correctly serialized on the remote client.
Images:
Broker/rpcclient only receives content string:
Object is properly serialized on the remote client before sending:
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (2 by maintainers)

Top Related StackOverflow Question
message payload is byte array we dont care what you send in there. message seperation is done by size in the header so we dont look at the payload at all.
retStr is not a CustomRestResponse. So your response is not valid. It’s not MQTT’s fault ( i think).
Can you double check it? Because it is really weird that MQTT is consistently only giving the “Content” property. There is highly likely an error in your code.
MQTT isn’t string data and doesn’t parse data, it just handles byte[]. The issue you are referencing looks like invalid code/logic.
@zornwal I assume it’s what your rcpclient.ExecuteAsync() returns => Only content and not a CustomRestResponse.