20

I send the following message with content type application/json:

enter image description here

However whene i get messages from the same RabbitMQ Web console, it shows the payload as String.

enter image description here

What am I doing wrong? Or am I fundamentally misunderstanding and the Payload is always of type String?

4 Answers 4

31

From the official docs:

AMQP messages also have a payload (the data that they carry), which AMQP brokers treat as an opaque byte array. The broker will not inspect or modify the payload. It is possible for messages to contain only attributes and no payload. It is common to use serialisation formats like JSON, Thrift, Protocol Buffers and MessagePack to serialize structured data in order to publish it as the message payload. AMQP peers typically use the "content-type" and "content-encoding" fields to communicate this information, but this is by convention only.

So basically, RabbitMQ has no knowledge on JSON, messages all are just byte arrays to it

3
  • 1
    This answer below is more consice : stackoverflow.com/a/60583488/5413849
    – Bill Somen
    Jun 27, 2020 at 17:34
  • 2
    The question was about understanding what payload is, which I answered. I dont see any problems here.
    – Alex Buyny
    Jun 29, 2020 at 0:55
  • 1
    yes, but I found the other answer more consice. It gets to the point. Its Ok to understand what's going on, but its more important to know how to address the issue
    – Bill Somen
    Jun 29, 2020 at 9:58
15

From NodeJS Context:


If we want to send JSON object as message, we may get the following error:

The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of Object

So, we can convert the JSON payload as string and parse it in the worker. We stringify the JSON object before sending the data the Queue-

let payloadAsString = JSON.stringify(payload);

And from worker's end, we can then JSON.parse

let payload = JSON.parse(msg.content.toString());
//then access the object as we normally do, i.e. :
let id = payload.id;
1
  • this one works for me and should be the accepted answer.
    – Bill Somen
    Jun 27, 2020 at 17:33
5

For anyone using .Net to send objects via RabbitMQ.

You have to serialise your JSON object to byte array, send via RabbitMQ then de-serialise after receiving. You can do this like this:

Install the Newtonsoft JSON library

using Newtonsoft.Json;

Create a model for your JSON object message (in this case AccountMessage)

Serialise your object into byte array like this:

 byte[] messagebuffer = Encoding.Default.GetBytes(JsonConvert.SerializeObject(accountMessage) );

After receiving the message data, you can de-serialise like this:

AccountMessage receivedMessage = JsonConvert.DeserializeObject<AccountMessage>(Encoding.UTF8.GetString(body));
3
  • Why do you think this question is about .NET?
    – user11044402
    Jul 11, 2019 at 13:04
  • 6
    I found this question when searching for how to send objects in Rabbit MQ, I use .Net, so someone else might find the solution I found useful here,. Jul 12, 2019 at 6:45
  • Why is it better to serialize Json into a byte array, and not into just a string?
    – AndCode
    Apr 21 at 6:46
2

from here

Content Type and Encoding

The content (MIME media) type and content encoding fields allow publishers communicate how message payload should be deserialized and decoded by consumers.

RabbitMQ does not validate or use these fields, it exists for applications and plugins to use and interpret.

by the way, using the rabbitMQ web gui, you use the words content_type, however in code (javascript confirmed), you use the key name contentType. it's a subtle difference, but enough to drive you crazy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.