question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sending JSON string as payload leads to loss of data

See original GitHub issue

Hello!

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: corelog Object is properly serialized on the remote client before sending: pilog

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
JanEggerscommented, Dec 5, 2018

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.

0reactions
NicoJuicycommented, Dec 27, 2018
var jsonResponse = JsonConvert.DeserializeObject<CustomRestResponse>(retStr); //Throws exception because retStr is not a CustomRestResponse now.

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sending JSON string as payload leads to loss of data #483
Hello! I'm experiencing some weird behaviour when sending a string in the JSON format in an RPC call. I have an RPC client...
Read more >
What is a JSON Injection and How to Prevent it?
Injection attacks can lead to loss of data, modification of data, and denial of service. As a result, it is listed as the...
Read more >
Passing a Python string into JSON payload
The reason your string isn't working is because you used double quotes " for the string instead of single quotes ' . Since...
Read more >
Reducing JSON Data Size
In this tutorial, we'll look at various ways of reducing the size of JSON in our Java applications.
Read more >
Difference between `json` and `data` parameter of the HTTP ...
When sending JSON data as the body of a POST request, the requests library in Python provides two options: using the json parameter...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found